您的位置:首页 > 其它

字符串替换问题

2012-09-03 16:36 260 查看
Replace all occurrence of the given pattern to ‘X’.

For example, given that the pattern=”abc”, replace “abcdeffdfegabcabc” with “XdeffdfegX”. Note that multiple occurrences of abc’s that are contiguous will be replaced with only one ‘X’.

First, it is not clear whether the problem mentions an in-place replacement or not, so be sure to ask this question during an interview. Many interview questions asked are purposely ambiguous. It is expected that the candidate ask thought-provoking questions
of the interviewer in order to better answer the question. Here, we will assume that it is an in-place replacement.

Hint:

If the problem seemed overly complex to you, you are in the wrong path. Try to break the problem into manageable pieces. Consider having a helper function called

bool isMatch(char *str, const char *pattern)


which returns true if pattern matches str starting from its first character.

Solution:

We should take advantage that the replacement is only one character in length. Assume that the pattern is at least one character in length, then the replacement’s length will never exceed the pattern’s length. This means that when we overwrite the existing
string with replacement, we would never overwrite characters that would be read next.

Two pointers are also used for an efficient in-place replacement, which traverses the string once without any extra memory.

bool isMatch(char *str, const char* pattern) {
while (*pattern)
if (*str++ != *pattern++)
return false;
return true;
}

void replace(char str[], const char *pattern) {
if (str == NULL || pattern == NULL) return;
char *pSlow = str, *pFast = str;
int pLen = strlen(pattern);
while (*pFast != '\0') {
bool matched = false;
while (isMatch(pFast, pattern)) {
matched = true;
pFast += pLen;
}
if (matched)
*pSlow++ = 'X';
// tricky case to handle here:
// pFast might be pointing to '\0',
// and you don't want to increment past it
if (*pFast != '\0')
*pSlow++ = *pFast++;  // *p++ = (*p)++
}
// don't forget to add a null character at the end!
*pSlow = '\0';
}


Test Cases:

Format is string, pattern = answer)
-----------------------------------
a, a = X
aa, aa = X
aa, a = X
aa, aaa = aa
abc, abc = X
abcabc, abc = X
abcabcabc, abc = X
abcaabcaabc, abc = XaXaX
abcaaabcaaabca, abc = XaaXaaXa
abcabcabababcabc, abc = XababX
abcabcabababcabcab, abc = XababXab
aabbaabbaaabbbaabb, aabb = XaXbX
aabbaabbaaabbbaabb, aaabb = aabbaabbXbaabb
aabbaabbaaabbbaaabb, aaabb = aabbaabbXbX
aabbaabbaaabbbaaabc, aaabb = aabbaabbXbaaabc
abcdeffdfegabcabc, abc = XdeffdfegX
abcdeffdfegabcabc, ab = XcdeffdfegXcXc
abcdeffdfegabcabc, a = XbcdeffdfegXbcXbc
abcdeffdfegabcab, abc = XdeffdfegXab
abcdeffdfegabcabcab, abc = XdeffdfegXab
abcdeffdfegabcaabcab, abc = XdeffdfegXaXab
abcdeffdfegabcaaaabcab, abc = XdeffdfegXaaaXab
aaaaaa, a = X
aaaaaa, aa = X
aaaaaa, aaaaaa = X
aaaaaa, aaaaaaa = aaaaaa
aabaababaaab, a = XbXbXbXb
aabaababaaa, a = XbXbXbX
aaaab, a = Xb
baaa, a = bX
aabaaabaab, aaa = aabXbaab
aabaaabaab, aa = XbXabXb
aabaaabaa, aa = XbXabX

http://www.leetcode.com/2010/11/microsoft-string-replacement-problem.html
如果用KMP算法的话,可以快一些,先用一个vector记录所有的匹配子串的起始位置,然后进行替换,替换的时候要注意判断两个子串是否是紧邻的,如果是替换一次即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: