I have sent hours trying to find out the reason why the method returns a wrong answer for this particular test case: "qrsvbspk". The method returns 6 instead of 5. I cannot figure out what is wrong. Plz help!
Here's my approach:
class Solution {
public int lengthOfLongestSubstring(String s) {
int i = 0;
int max_count = -1;
int count = 0;
if(s.length() == 0)
return 0;
HashSet<Character> set = new HashSet();
int k = 0;
while(k < s.length()){
i = k;
while(i < s.length() && !set.contains(s.charAt(i))){
count++;
set.add(s.charAt(i++));
}
if(count >= max_count){
max_count = count;
set.clear();
count = 0;
}
k++;
}
return max_count;
}
}