没有重复字符的最长子串

转自leetcode

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

Hint:

Is there a better way other than brute force? Consider the kind of data structure that can improve the run time complexity. An ideal solution requires only a one-time linear scan.

Online Judge

This problem is available atOnline Judge.Head over thereand it will judge your solution. Currently only able to compile C++ code. If you are using other languages, you can still verify your solution by looking at the judge’s test cases and its expected output.

Solution:

How can we can look up if a character had existed in the substring instantaneously? The answer is using a simple table to store the characters that have appeared. Make sure you communicate with your interviewer if the string can have characters other than ‘a’-'z’. (ie, Digits? Upper case letter? Does it contain ASCII characters only? Or even unicode character sets?)

As you traverse through the string, update by using its ASCII value as index to the table. If the string only contains ‘a’-'z’, you could save space by using a table of size 26 only. Assuming c is the character, then c-’a’ will give you a value of 0-25 which can be used to index the table directly.

The next question is to ask yourself what happens when you found a repeated character? For example, if the string is “abcdcedf”, what happens when you reach the second appearance of ‘c’?

When you have found a repeated character (let’s say at indexj), it means that the current substring (excluding the repeated character of course) is a potential maximum, so update the maximum if necessary. It also means that the repeated character must have appeared before at an indexi, whereiis less thanj.

Since you know that all substrings that start before or at indexiwould be less than your current maximum, you can safely start to look for the next substring with head which starts exactly at indexi+1.

Therefore, you would need two indices to record the head and the tail of the current substring. Since i and j both traverse at mostnsteps, the worst case would be 2nsteps, which the run time complexity must be O(n).

Below is the implementation in C++. Beware of the common mistake of not updating the maximum after the main loop, which is easy to forget.
[cpp]view plaincopy
1. intlengthOfLongestSubstring(string s) {
2. intn = s.length();
3. inti = 0, j = 0;
4. intmaxLen = 0;
5. boolexist[256] = {false};
6. while(j < n) {
7. if(exist[s[j]]) {
8. maxLen = max(maxLen, j-i);
9. while(s[i] != s[j]) {
10. exist[s[i]] =false;
11. i++;
12. }
13. i++;
14. j++;
15. }else{
16. exist[s[j]] =true;
17. j++;
18. }
19. }
20. maxLen = max(maxLen, n-i);
21. returnmaxLen;
22. }
原文链接: https://www.cnblogs.com/lightBase/archive/2012/10/31/2748680.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/67734

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午12:57
下一篇 2023年2月9日 下午12:58

相关推荐