Skip to content

Instantly share code, notes, and snippets.

@zzandland
Created May 7, 2025 14:58
Show Gist options
  • Save zzandland/5d93c650a086f59fa3522e2593c1fa67 to your computer and use it in GitHub Desktop.
Save zzandland/5d93c650a086f59fa3522e2593c1fa67 to your computer and use it in GitHub Desktop.
isBothPrefixAndSuffix
// checks if target string has key string as both prefix and suffix
bool isBothPrefixAndSuffix(string &target, string &key) {
int n = target.length(), m = key.length();
// target must be at least twice as long as key
if (n < m * 2) {
return false;
}
int lt = 0, rt = target.length() - 1, lk = 0, rk = key.length() - 1;
while (lk < m) {
if (key[lk] != target[lt] || key[rk] != target[rt]) {
return false;
}
++lt; ++lk; --rt; --rk;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment