It's my solution in O(n)


  • 0
    F

    It's my solution in Java 13 lines.

    import java.util.HashMap;
    class Solution {
    	public int lengthOfLongestSubstring(String s) {
    		int result = 0, len = 0, start = 0;
    		HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    		for (int i = 0; i < s.length(); i++) {
    			len++;
    			if (map.containsKey(s.charAt(i)) == true && map.get(s.charAt(i)) >= start) {
    				start = map.get(s.charAt(i)) + 1;
    				len = i - start + 1;
    			}
    			map.put(s.charAt(i), i);
    			if (len > result)
    				result = len;
    		}
    		return result;
    	}
    }
    

Log in to reply
 

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