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.

 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         int len = s.length();
 4         if(len<=1) return len;
 5         int [] index = new int[26];
 6         int max = 0;
 7         int count = 0;
 8         for(int i=0;i<len;i++,count++){
 9             if(index[s.charAt(i)-'a']!=0){
10                 i = index[s.charAt(i)-'a'];
11                 index = new int [26];
12                 max = Math.max(max,count);
13                 count =0;
14             }
15             index[s.charAt(i)-'a'] = i+1;
16         }
17         return Math.max(max,count);
18     }
19 }
View Code

 check[s.charAt(i)-'a'] = i+1; because index from 0

for(int i=0;i<s.length();i++,count++)

posted @ 2014-02-22 13:21  krunning  阅读(139)  评论(0编辑  收藏  举报