程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Longest Substring Without Repeating Characters 最長不重復字符的字串 @L

Longest Substring Without Repeating Characters 最長不重復字符的字串 @L

編輯:C++入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved