Simple JAVA solution , beats 91 %


  • 5
    P
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            int max = 0, i =0 , j = 0,index;
            int[] hm = new int[128];
            while(j<s.length()){
                if(hm[s.charAt(j)] == 0){
                    hm[s.charAt(j)] = j+1;
                    j++;
                    if((j-i) > max)
                        max = j-i;
                }
                else {
                    index = hm[s.charAt(j)];
                    hm[s.charAt(j)] = 0;
                    if (i < index)
                        i = index;
                }
            }
            return max;
        }
    }

  • 0
    U
    This post is deleted!

Log in to reply
 

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