Non-DP solution, running time O(n) and memory O(n)


#1

Comment body goes here.public class Solution {
final int INT_FALSE = 0;
final int INT_TRUE = 1;
public int anytwo(String A) {
Set seenBefore = new HashSet<>();
boolean seenTwice = false;
for (char c: A.toCharArray()) {
if (!seenBefore.contains©) {
seenBefore.add©;
} else if (seenTwice) {
return INT_TRUE;
} else {
seenTwice = true;
seenBefore.clear();
seenBefore.add©;
int i = A.indexOf© + 1;
while (A.charAt(i) != c) {
seenBefore.add(A.charAt(i));
i++;
}
}
}
return INT_FALSE;
}
}