您的位置:首页 > 其它

Longest Substring Without Repeating Characters 最长不重复字符的字串 @LeetCode

2014-01-05 01:56 387 查看
Method 1 (Simple)
We can consider all substrings one by one and check for each substring whether it contains all unique characters or not. There will be n*(n+1)/2 substrings. Whether a substirng contains all unique characters or not can be checked in linear time by scanning it from left to right and keeping a map of visited characters. Time complexity of this solution would be O(n^3).Method 2 (Linear Time)
Let us talk about the linear time solution now. This solution uses extra space to store the last indexes of already visited characters. The idea is to scan the string from left to right, keep track of the maximum length Non-Repeating Character Substring (NRCS) seen so far. Let the maximum length be max_len. When we traverse the string, we also keep track of length of the current NRCS using cur_len variable. For every new character, we look for it in already processed part of the string (A temp array called visited[] is used for this purpose). If it is not present, then we increase the cur_len by 1. If present, then there are two cases:a) The previous instance of character is not part of current NRCS (The NRCS which is under process). In this case, we need to simply increase cur_len by 1.
b) If the previous instance is part of the current NRCS, then our current NRCS changes. It becomes the substring staring from the next character of previous instance to currently scanned character. We also need to compare cur_len and max_len, before changing current NRCS (or changing cur_len).

http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/

package Level3;

import java.util.Arrays;

/**
* Longest Substring Without Repeating Characters
*
* 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.
*
*/
public class S3 {

public static void main(String[] args) {
String s = "abcbad";
System.out.println(lengthOfLongestSubstring(s));
}

// http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/ // O(n), O(1)
public static int lengthOfLongestSubstring(String s) {
if(s == null || s.length()==0){
return 0;
}

// visited[char's ASCII] = char's index
int[] visited = new int[256];
Arrays.fill(visited, -1);

int curLen = 1;
int maxLen = 1;
int prevIndex = 0;
visited[s.charAt(0)] = 0;

for(int i=1; i<s.length(); i++){
prevIndex = visited[s.charAt(i)]; // 之前存储过的index
// 如果是第一次出现,或者不在当前考虑的字串内 如当访问第二个a时对于第一个a就不在考虑范围
if(prevIndex == -1 || prevIndex+curLen<i){
curLen++; // 在旧字串上增加
}else{ // 如b
maxLen = Math.max(maxLen, curLen);
curLen = i - prevIndex; // 建立新的字串
}
visited[s.charAt(i)] = i; // 更新
}
// 最后一次
maxLen = Math.max(maxLen, curLen);

return maxLen;
}

}


又是一道很重要的题目:
方法:双指针window区间法
首先brute force的时间复杂度是O(n^3), 对每个substring都看看是不是有重复的字符,找出其中最长的,复杂度非常高。优化一些的思路是稍微动态规划一下,每次定一个起点,然后从起点走到有重复字符位置,过程用一个HashSet维护当前字符集,认为是constant操作,这样算法要进行两层循环,复杂度是O(n^2)。

最后,我们介绍一种线性的算法,也是这类题目最常见的方法。基本思路是维护一个窗口,每次关注窗口中的字符串,在每次判断中,左指针和右指针选择其一向前移动。同样是维护一个HashSet, 正常情况下移动右指针,如果没有出现重复则继续移动右指针,如果发现重复字符,则说明当前窗口中的串已经不满足要求,继续移动右指针不可能得到更好的结果,
此时固定右指针,移动左指针,直到不再有重复字符为止(即找到和右指针相同的值,然后跳过它),中间跳过的这些串中不会有更好的结果,因为他们不是重复就是更短。
因为左指针和右指针都只向前,所以两个窗口都对每个元素访问不超过一遍,因此时间复杂度为O(2*n)=O(n),是线性算法。空间复杂度为HashSet的size,也是O(n).
http://codeganker.blogspot.com/2014/02/longest-substring-without-repeating.html
同类题:
Substring with Concatenation of All WordsMinimum Window Substring

import java.util.*;

public class Solution {
public int lengthOfLongestSubstring(String s) {
HashSet<Character> set = new HashSet<Character>();
int left = 0, right = 0;
int max = 0;

while(right < s.length()) {
if(!set.contains(s.charAt(right))) { // can add to the window
set.add(s.charAt(right));
} else { // cannot add to window, because of duplicate
max = Math.max(max, right-left);

while(s.charAt(left) != s.charAt(right)) { // until find the duplicate character
set.remove(s.charAt(left));
left++;
}
left++; // skip the duplicate
}
right++;
}

max = Math.max(max, right-left);
return max;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐