package general;

import java.util.HashSet;
import java.util.Set;

public class LongestSubstringWithNoRepetition {

	public static int lengthOfLongestSubstring(String s) {
		if (s == null || s.isEmpty()) {
			return 0;
		}
		Set<Character> seen = new HashSet<Character>();

		int windowStart = 0;
		int windowEnd = 0;
		int maxWindowStart = -1;
		int maxWindowEnd = -1;
		while (windowEnd < s.length()) {
			if (seen.contains(s.charAt(windowEnd))) {
				for (; windowStart <= windowEnd - 1
						&& s.charAt(windowStart) != s.charAt(windowEnd); windowStart++) {
					seen.remove(s.charAt(windowStart));
				}

				windowStart++;
			} else {
				seen.add(s.charAt(windowEnd));
			}

			if (exceeded(windowStart, windowEnd, maxWindowStart, maxWindowEnd)) {
				maxWindowStart = windowStart;
				maxWindowEnd = windowEnd;
			}

			windowEnd++;
		}

		return maxWindowEnd - maxWindowStart + 1;
	}

	private static boolean exceeded(int windowStart, int windowEnd,
			int maxWindowStart, int maxWindowEnd) {
		if (maxWindowStart == -1 || maxWindowEnd == -1) {
			return true;
		}

		return (windowEnd - windowStart) > (maxWindowEnd - maxWindowStart);
	}
}
@@ -603,16 +603,115 @@
 %0A%09%09%09%09%7D%0A%0A
+%09%09%09%09// we don't want to include this character because it already%0A%09%09%09%09// exists in the 'seen' set.%0A
 %09%09%09%09wind
@@ -1239,10 +1239,125 @@
 art);%0A%09%7D
+%0A%0A%09public static void main(String%5B%5D args) %7B%0A%09%09System.out.println(lengthOfLongestSubstring(%22cbadaxyzmnopqrhij%22));%0A%09%7D
 %0A%7D

Add comment.