`
xitonga
  • 浏览: 579612 次
文章分类
社区版块
存档分类
最新评论

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


分享到:
评论

相关推荐

    LeetCode3 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with...

    longest-substring-without-repeating-characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with...

    js代码-3. Longest Substring Without Repeating Characters

    js代码-3. Longest Substring Without Repeating Characters

    没有重复字符最长子串

    Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3.

    leetcode分类-leetcode:leetcode问题的代码

    leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...

    程序员面试宝典LeetCode刷题手册

    3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17...

    javalruleetcode-Leetcode:力码解决方案

    (无重复字符的最长子串) Medium Dual Pointer (Sliding Window), Hash Table 4 Median of Two Sorted Arrays (寻找两个有序数组的中位数) Hard Divide and Conquer 5 Longest Palindromic Substring (最长回文子串) ...

    lrucacheleetcode-leetcode-1:leetcode-1

    最长没有重复字符的子序列 记录各字符最近一次出现的位置 4. Median of Two Sorted Arrays 求两有序数列的中位数,可泛化为求两有序数列的第k位数,二分法 5. Longest Palindromic Substring 最长回文子串,补符号,...

    leetcode答案-LeetCode-Longest_Substring_Without_Repeating_Characters:Leet

    答案LeetCode-Longest_Substring_Without_Repeating_Characters 这是LeetCode上“最长子串无重复字符”问题的解决方案。 问题描述:给定一个字符串,求没有重复字符的最长子串的长度。 示例 1:输入:“abcabcbb” ...

    javalruleetcode-LeetCode:LeetCode算法问题

    java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...Characters LeetCode 13 Roman to Integer LeetCode 6 Zi

    leetcode中文版-LeetCode:LeetcodeC++/Java

    无重复字符的最长子串 string 4 LMedian of Two Sorted Arrays 两个排序数组的中位数 ary,binary search,dive and conquer 5 Longest Palindromic Substring 最长回文子串 string,dp 8 String to Integer(atoi) 字符...

    leetcode2sumc-LeetCode-3.Longest_Substring_Without_Repeating_Characters

    LeetCode-3.Longest_Substring_Without_Repeating_Characters 给定一个字符串,找出没有重复字符的最长子字符串的长度。 示例 1: 输入:“abcabcbb” 输出:3 解释:答案是“abc”,长度为 3。 解决方案 Python3:...

    leetcodepython001-leetcode:https://leetcode.com/的解决方案

    leetcode Python 001 力码 ├── Algorithms │  ├── cpp │  │  ├── 001 - Two Sum.cpp │  │  ├── 002 - Add Two Numbers.cpp │  │  ├── 003 - Longest Substring Without Repeating ...

    LeetCode5 Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本

    longest-substring-without-repeating-characters:无重复字符的最长子串的长度(http

    最长子串无重复字符没有重复字符的最长子串的长度( )

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode跳跃-LeCode:乐科

    无重复字符的最长子串 4. Median of Two Sorted Arrays 寻找两个有序数组的中位数 5. Longest Palindromic Substring 最长回文子串 6. ZigZag Conversion Z字型变换 7. Reverse Integer 整数反转 8. String to ...

    leetcode跳跃-leetcode:leetcode一天一次

    无重复字符的最长子串:3. Longest Substring Without Repeating Characters - 最小跳跃步数问题 - 动态规划 + 循环策略优化:45. Jump Game II - 二叉树 前序遍历判断二叉树:98. Validate Binary Search Tree - 二...

    leetcode题库-LeetCode:力码

    无重复字符的最长字串 Longest Substring Without Repeating Characters.cpp 4 寻找两个有序数组的中位数 Median of Two Sorted Arrays.cpp 5 最长回文子串 Longest Palindromic Substring.cpp 7 整数反转 Reverse ...

Global site tag (gtag.js) - Google Analytics