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 the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
Subscribe to see which companies asked this question
My Solution:
public class Solution {
public int lengthOfLongestSubstring(String s) {
int max=0;
int i,j;
for(i=0;i+max<s.length();++i){
boolean[] flag=new boolean[128];
j=i;
int len=0;
while(j<s.length()){
if(!flag[s.charAt(j)]){
flag[s.charAt(j)]=true;
len++;
j++;
continue;
}
else
break;
}
max=max>len?max:len;
if(j==s.length())
break;
}
return max;
}
}