my java solution


  • 0
    S
    import java.util.HashMap;
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            HashMap<Character, Integer> hmap = new HashMap<Character, Integer>();
            int start = 0, end = 0;
            int maxLen = 0;
            while(end < s.length()) {
                if(hmap.containsKey(s.charAt(end)) && (hmap.get(s.charAt(end)) >= start)) {
                    start = hmap.get(s.charAt(end)) + 1;
                } else if(((end - start) + 1) > maxLen){
                    //not in the table => put the k,v to the hashtable
                    //and update the max length
                    maxLen = (end-start+1);
                }
                hmap.put(s.charAt(end), end);
                end += 1;
            }
            return maxLen;
        }
    }
    

Log in to reply
 

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