Simple Java Solution Two pointers


  • 0
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            int i = 0, j = 0, maxl = 0;
            boolean[] table = new boolean[128];
            while(j < s.length()){
                while(table[(int)s.charAt(j)]){
                    table[(int)s.charAt(i)] = false;
                    i++;
                }
                table[(int)s.charAt(j)] = true;
                maxl = j-i+1 > maxl? j-i+1 : maxl;
                j++;
            }
            return maxl;
        }
    }

Log in to reply
 

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