从字符串中删除重复的字符
2013-02-15 02:01
435 查看
设计一个算法去移除字符串中重复的字符,但是只可以使用一个或两个变量的额外空间。
Design an algorithm and write code to remove the duplicate characters in a
string without using any additional buffer. NOTE: One or two additional variables
is fine. An extra copy of the array is not. pg 22
为算法写出测试用例。
FOLLOW UP
Write the test cases for this method.
解:
SOLUTION
First, ask yourself, what does the interviewer mean by an additional buffer? Can we use an
additional array of constant size?
不用额外空间的解法:
将原数组分为两边,一边为不重复的字符串a,另一边为要处理的字符串b。
对于b中的字符,检查他是否在a中出现,如果出现了,处理b中的下一个字符,如果没有出现,将其添加到a中。
时间复杂度为O(N2).
初始时a中有一个字符。
Algorithm—No (Large) Additional Memory:
1. For each character, check if it is duplicate of already found characters.
2. Skip duplicate characters and update the non duplicate characters.
Time complexity is O(N2).
Test Cases:
1. String does not contain any duplicates, e.g.: abcd
2. String contains all duplicates, e.g.: aaaa
3. Null string
4. String with all continuous duplicates, e.g.: aaabbb
5. String with non-contiguous duplicate, e.g.: abababa
使用额外空间的算法:
使用hit数组记录字符是否重复,如果为true表示在未重复的字符串中。
Algorithm—With Additional Memory of Constant Size
1. String does not contain any duplicates, e.g.: abcd
2. String contains all duplicates, e.g.: aaaa
3. Null string
4. Empty string
5. String with all continuous duplicates, e.g.: aaabbb
6. String with non-contiguous duplicate, e.g.: abababa
Design an algorithm and write code to remove the duplicate characters in a
string without using any additional buffer. NOTE: One or two additional variables
is fine. An extra copy of the array is not. pg 22
为算法写出测试用例。
FOLLOW UP
Write the test cases for this method.
解:
SOLUTION
First, ask yourself, what does the interviewer mean by an additional buffer? Can we use an
additional array of constant size?
不用额外空间的解法:
将原数组分为两边,一边为不重复的字符串a,另一边为要处理的字符串b。
对于b中的字符,检查他是否在a中出现,如果出现了,处理b中的下一个字符,如果没有出现,将其添加到a中。
时间复杂度为O(N2).
初始时a中有一个字符。
Algorithm—No (Large) Additional Memory:
1. For each character, check if it is duplicate of already found characters.
2. Skip duplicate characters and update the non duplicate characters.
Time complexity is O(N2).
void removeDuplicates(char *str) { if (!str) return; int len = strlen(str); if (len < 2) return; int tail = 1; for (int i = 1; i < len; ++i) { int j; for (j = 0; j < tail; ++j) if (str[i] == str[j]) break; if (j == tail) { str[tail] = str[i]; ++tail; } } str[tail] = 0; }
Test Cases:
1. String does not contain any duplicates, e.g.: abcd
2. String contains all duplicates, e.g.: aaaa
3. Null string
4. String with all continuous duplicates, e.g.: aaabbb
5. String with non-contiguous duplicate, e.g.: abababa
使用额外空间的算法:
使用hit数组记录字符是否重复,如果为true表示在未重复的字符串中。
Algorithm—With Additional Memory of Constant Size
void removeDuplicatesEff(char *str) { if (!str) return; int len = strlen(str); if (len < 2) return; bool hit[256]; for(int i = 0; i < 256; ++i) hit[i] = false; hit[str[0]] = true; int tail = 1; for (int i = 1; i < len; ++i) { if (!hit[str[i]]) { str[tail] = str[i]; ++tail; hit[str[i]] = true; } } str[tail] = 0; }Test Cases:
1. String does not contain any duplicates, e.g.: abcd
2. String contains all duplicates, e.g.: aaaa
3. Null string
4. Empty string
5. String with all continuous duplicates, e.g.: aaabbb
6. String with non-contiguous duplicate, e.g.: abababa
相关文章推荐
- 输入字符串,删除重复次数最多的字符
- 剑指Offer----面试题35:扩展----删除字符串中所有重复出现的字符 & 变位词
- [每天一题]删除字符串中的重复字符(不要使用额外空间)
- 删除字符串中重复的字符
- 如何删除字符串中重复的字符
- 删除字符串中重复的字符
- 删除字符串中的重复字符(字符串为ASCII)
- 删除字符串中重复的字符
- [算法] 定义一个函数,删除字符串中所有重复出现的字符。
- 删除字符串中重复字符
- HWOJ 删除字符串中重复出现的字符
- 华为:输入一个字符串,删除重复前面的字符,其余字符按原样顺序输出
- Linux基础day4,shell编程,感觉比较经典的:删除字符串中重复字符
- 字符串处理: 如何删除字符串中重复的字符
- c 删除一个字符串中重复的字符
- JavaScript字符串删除重复字符的方法
- leetcode——Remove Duplicates from Sorted List II 删除排序字符串中重复字符(AC)
- Perl: 删除字符串中的重复字符
- bitmap 删除字符串中重复字符
- 删除小写字母字符串中重复字符