AUG!!! Happy Two Pointers, time O(n), space O(n), not super fast but very basic


  • 1
    //The code explains itself, if you got confused with anything, comment bellow.
    public static int lengthOfLongestSubstring(String s) {
                    int pointer1 = 0;
    		int pointer2 = 0;
    		int res = 0;
    		HashMap<Character, Integer> map = new HashMap<>();
    		while(pointer2 != s.length())
    		{
    			char ch = s.charAt(pointer2);
    			if(map.containsKey(ch) && map.get(ch) >= pointer1)
    			{
    				pointer1 = map.get(ch)+1;
    				
    			}
    			else
    			{
    				res = (pointer2 - pointer1 + 1 > res)?pointer2 - pointer1 + 1: res;
    			}
    			map.put(ch,pointer2);
    			pointer2++;
    		}
    		return res;
        
        }
    

Log in to reply
 

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