您的位置:首页 > 其它

[Leetcode] - Longest Substring Without Repeating Characters

2014-03-07 07:41 337 查看
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.

这两天一直在努力研究各种substring相关的问题,于是在Leetcode上面找到了这道题,复习一下当时的想法。具体思路是这样的,用一个HashMap来记录每个出现的字符和它们在字符串中所出现的位置,也就是index。所以说,这个HashMap应该被初始化为HashMap<Character, Integer>。用start和end两个变量来保存当前NRCS(Non-Repeating Character
Substring)的起始和终止位置,说白了就是一个窗口,start初始为0,end用作循环里面自增的变量,end的大小等于string长度的时候结束循环。最后用len这个变量记录最长的NRCS的长度,将其初始化为0,因为NRCS的长度至少为1。
有了这些东西之后,我们就可以开始遍历这个string求答案了。对于每个字符,如果HashMap中没有,就将它以及它的index放进去。如果HashMap中已经有了这个Key的话,我们需要做如下几件事情:
1. 首先,把这个字符以前出现的index从HashMap里面拿出来,存在变量index里面。
2. 然后,比较end-start和len的大小,如果需要的话,更新len的值。(注意:end-start得到的值就是当前NRCS的长度)
3. 接着,检查字符串在start和index间的所有字符,对于每个字符,把HashMap中对应的key-value pair删除。
4. 最后,将start设置为这个index加1的位置。
还有一点需要注意的是,如果这个字符串没有重复的字符,那么上面的方法将不会更新len的值。所以,在循环结束之后,还需要比较一次end-start和len的大小,需要时update一下len的值。
综上所述,time complexity是O(n)。由于用了HashMap,所以space complexity是O(d),这里面d是字符串中不重复字符的个数。

代码如下:
public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s==null || s.length()==0) return 0;
if(s.length()==1) return 1;

HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int start=0, end=1, len=Integer.MIN_VALUE;
map.put(s.charAt(start), 0);
while(end < s.length()) {
if(map.containsKey(s.charAt(end))) { // find duplicate characters
len = Math.max(len, end-start); // update the maximum length if necessary
int index = map.get(s.charAt(end));
for(int k=start; k<=index; k++) {
map.remove(s.charAt(k));
}
start = index+1;
}
map.put(s.charAt(end), end);
end++;
}
len = Math.max(len, end-start); // in case there is no duplicate in this string
return len;
}
}

捎带手的复习一下substring,subsequence,prefix,suffix等的定义,下面是wikipedia里面的一段话:

substring of a string 

 is
another string 

 that occurs "in" 

.
For example, "the best of" is a substring of "It was the best of times". This is not to be confused with subsequence,
which is a generalization of substring. For example, "Itwastimes" is a subsequence of "It was the best of times",
but not a substring.

Prefix and suffix are refinements of substring. A prefix of a string 

 is a substring of 

 that
occurs at the beginning of 

. A suffix of a string 

is
a substring that occurs at the end of 

.
Not including the empty substring, the number of substrings of a string of length 

 where
symbols only occur once, is the number of ways to choose two distinct places between symbols to start/end the substring. Including the very beginning and very end of the string, there are 

 such
places. So there are 

 non-empty
substrings.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: