
package javaProbs;

import java.util.HashMap;

public class Solution_003_LongestSubstring
{

	public static int lengthOfLongestSubstring( String s )
	{
		// Initial null check
		if ( s.length() == 0 ) return 0;
		
		// Key = character, Value = index
		HashMap<Character, Integer> map = new HashMap<Character, Integer>();
		
		int longest = 0;

		int j = 0; // Place marker for when a duplicate char is found
		
		for ( int i = 0; i < s.length(); i++ )
		{
			if ( map.containsKey(s.charAt(i)) )
			{
				j = Math.max(j, map.get(s.charAt(i)) + 1);
			}
			
			map.put(s.charAt(i), i);
			
			longest = Math.max(longest, i - j + 1);
		}
		return longest;
	}

	public static void main( String[] args )
	{
		int length = 0;
		String word = "dvdfldpam";

		length = lengthOfLongestSubstring(word);

		System.out.println(length);
	}

}
