I am attempting to do the longest substring without repeating characters problem in Java. I am trying to do two nested loop, and use a counter to keep track of how many characters are not repeated. When it finds a repeated character, it will reset the counter and break the loop. As of now, it is outputing 2 for this test case "abcabcbb", but I am not sure why.
class Solution { public int lengthOfLongestSubstring(String s) { if (s.length() == 0) return 0; if (s.length() == 1) return 1; int max = 1; // if (s.charAt(0) != s.charAt(1)) max = 2; int counter = 1; String a = "" + s.charAt(0); Character b; Character c; for (int i = 1;i<s.length()-1;i++){ b = s.charAt(i); for (int j = 0; j< s.length()-i; j++){ c = a.charAt(j); if (b.equals(c)){ counter = 1; a = "" + s.charAt(i); break; } else{ counter++; a = a + s.charAt(i); if (max < counter) max = counter; } } } return max; } }