Why i got wrong result?


  • 0
    Y

    please help me out here, thanks a lot.

    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            char[] ch=s.toCharArray();
            int i=0;int len=0;
            int max=0;
            Map<Character, Integer> map= new HashMap<Character, Integer>();
            while(i<s.length())
            {  if(!map.containsKey(ch[i]))
              {  map.put(ch[i],i);
                  i++;
                  len++;
              }
               else
               {  int oldi=map.get(ch[i]);
                  map.put(ch[i],i);
                  if(max<len) max=len;
                  len=len-oldi+1;
                  i++;
                  
               }
              
            }
            return max;
        }
    }

Log in to reply
 

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