12 line java solution, no hashmap


  • 1
    C
    public int lengthOfLongestSubstring(String s) {
            int[] map = new int[128];
            int begin=0, end=0, len=0;
            while(end < s.length()){
                if(map[s.charAt(end)] == 0){
                    len = Math.max(len, end-begin+1);
                }else{
                    while(s.charAt(begin++)!=s.charAt(end)) 
                        map[s.charAt(begin-1)]--;
                    map[s.charAt(begin-1)]--;
                }
                map[s.charAt(end++)]++;
            }
            return len;
        }
    

Log in to reply
 

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