Created
May 7, 2025 14:58
-
-
Save zzandland/5d93c650a086f59fa3522e2593c1fa67 to your computer and use it in GitHub Desktop.
isBothPrefixAndSuffix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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