Given a string, find the longest substring without any repeating characters and return the length of it. The input string is guaranteed to be not null.
For example, the longest substring without repeating letters for "bcdfbd" is "bcdf", we should return 4 in this case.
public class Solution { public int longest(String input) { // Write your solution here if (input.length() == 0) { return 0; } char[] charArr = input.toCharArray(); Set<Character> set = new HashSet<>(); int res = 0; int start = 0, i = 0; while (i < charArr.length) { char cur = charArr[i]; if (!set.contains(cur)) { set.add(cur); res = Math.max(res, i - start + 1); i += 1; } else { set.remove(charArr[start]); start += 1; } } return res; } }