12-line Java Solution using arrays


  • 0
    D
    public int lengthOfLongestSubstring(String s) {
            if (s.length() == 0 || s == null){return 0;}
            int start = 0;
            int end = 0;
            int[] map = new int[128];
            int max = Integer.MIN_VALUE;
            char[] arr = s.toCharArray();
            while (end < arr.length){
                while(map[arr[end]] > 0) {
                    map[arr[start++]]--;
                }
                map[arr[end++]]++;
                max = Math.max(max, end - start);
            }
            return max;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.