字符串替换问题
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
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.
Test Cases:
http://www.leetcode.com/2010/11/microsoft-string-replacement-problem.html
如果用KMP算法的话,可以快一些,先用一个vector记录所有的匹配子串的起始位置,然后进行替换,替换的时候要注意判断两个子串是否是紧邻的,如果是替换一次即可。
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记录所有的匹配子串的起始位置,然后进行替换,替换的时候要注意判断两个子串是否是紧邻的,如果是替换一次即可。
相关文章推荐
- js提交文本内容,无法提交到服务器问题解决和js替换字符串问题
- string类学习字符串中的子串出现次数与替换的问题
- js子字符串替换问题
- 解决replaceAll替换字符串中包括特殊符号时出现的问题
- 字符串 批量 替换 问题
- 简单的C语言字符串替换问题——
- 剑指offer剖析__空格替换字符串问题
- 面试之路(20)-字符串中空格替换一类问题详解
- Jquery、JavaScript 只能替换第一个字符串的问题
- python批量替换多文件字符串问题详解
- 缓存需要注意的问题以及使用.net正则替换字符串的方法
- 面试之路(20)-字符串中空格替换一类问题详解
- 字符串替换问题
- Emacs正则表达式替换时替换字符串中含有回车符无法替换的问题
- str_replace字符串替换要注意的问题
- 字符串空格替换问题
- php字符串中双引号替换问题
- Regex.Replace替换字符串要注意的问题
- 面试之路(20)-字符串中空格替换一类问题详解
- C#对于字符串的处理类(剪裁、过滤危险字符、替换sql中有问题符号等)