最长不含重复字符的子字符串

输入一个字符串(只包含 a~z 的字符),求其最长不含重复字符的子字符串的长度。例如对于 arabcacfr,最长不含重复字符的子字符串为 acfr,长度为 4。

 

java:

 1     public static int longestSubStringWithoutDuplication(String str) {
 2         int curLen = 0 ;
 3         int maxLen = 0 ;
 4         int[] preIndex = new int[26] ;
 5         for(int i = 0 ; i < 26 ; i++){
 6             preIndex[i] = -1;
 7         }
 8         for(int i = 0 ; i < str.length() ; i++){
 9             int c = str.charAt(i) - 'a' ;
10             int pre = preIndex[c] ;
11             if (pre == -1 || i - pre > curLen){
12                 curLen++ ;
13             }else{
14                 curLen = i - pre ;
15             }
16             preIndex[c] = i ;
17             maxLen = Math.max(maxLen,curLen) ;
18         }
19         return maxLen ;
20     }

 

posted @ 2019-03-21 16:57  __Meng  阅读(592)  评论(0编辑  收藏  举报