/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
//abcabcdbd
//Sliding window solution Time: O(n), Space: O(1)
{
System.
out.
println(getLongestStringWithoutRepetitions
("abcabcdbd")); System.
out.
println(getLongestStringWithoutRepetitions
("")); System.
out.
println(getLongestStringWithoutRepetitions
("aaabbb")); System.
out.
println(getLongestStringWithoutRepetitions
("aaa")); System.
out.
println(getLongestStringWithoutRepetitions
("abc")); System.
out.
println(getLongestStringWithoutRepetitions
("abbcdecb")); }
private static int getLongestStringWithoutRepetitions
(String input
) { int start=0, end=0, result=0;
int[] count = new int[26];
while(end<input.length()) {
count[(int)(input.charAt(end)-'a')]++;
if(count[(int)(input.charAt(end)-'a')]>1) {
while(start<=end) {
start++;
count[(int)(input.charAt(start-1)-'a')]--;
if(count[(int)(input.charAt(start-1)-'a')]==1) {
break;
}
}
}
result
= Math.
max(result, end
-start
+1); end++;
}
return result;
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCS8vYWJjYWJjZGJkIAoJLy9TbGlkaW5nIHdpbmRvdyBzb2x1dGlvbiBUaW1lOiBPKG4pLCBTcGFjZTogTygxKQoJCglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24KCXsKCQlTeXN0ZW0ub3V0LnByaW50bG4oZ2V0TG9uZ2VzdFN0cmluZ1dpdGhvdXRSZXBldGl0aW9ucygiYWJjYWJjZGJkIik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