【转载】【leetcode】Longest Substring Without Repeating Characters
来源:互联网 发布:网络舆情试题 编辑:程序博客网 时间:2024/06/05 14:39
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 index i+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 most n steps, the worst case would be 2n steps, which the run time complexity must beO(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.
class Solution {public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int n=s.length(); bool exist[256]={false}; int i=0,j=0,maxLen=0; while(j<n) { if(exist[s[j]]) { maxLen=max(maxLen,j-i); while(s[i]!=s[j]) { exist[s[i]]=false; i++; } i++; j++; } else { exist[s[j]]=true; j++; } } return max(maxLen,n-i); }};
- 【转载】【leetcode】Longest Substring Without Repeating Characters
- 转载:Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- 排序算法实现
- Android工程改名出现错误
- SpringMVC注解入门(一)
- 数据库事务嵌套
- 编程珠玑之第一章习题5:k趟算法给1000w数据排序的测试用例
- 【转载】【leetcode】Longest Substring Without Repeating Characters
- 动态规划之最长子序列和最长子串
- Java案例
- 解决库链接时的相互依赖
- VS2008中CDockablePane停靠框
- 猜数字游戏的实现
- 如何解决"应用程序无法启动,因为应用程序的并行配置不正确"问题
- 为 android的系统控件添加属性
- linux 从文件系统方面理解软链接、硬链接