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

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;
    }
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值