没有重复字符的最长子串

转自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 at Online Judge.Head over there and 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 index j), 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 index i, where i is less than j.

Since you know that all substrings that start before or at index i would 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 mostn steps, 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 plain copy
  1. int lengthOfLongestSubstring(string s) {  
  2.   int n = s.length();  
  3.   int i = 0, j = 0;  
  4.   int maxLen = 0;  
  5.   bool exist[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.   return maxLen;  
  22. }  

转载于:https://www.cnblogs.com/lightBase/archive/2012/10/31/2748680.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值