educative.io

Educative

Would it be correct and still effective to solve it this way

private static int findLength(char[] arr) {
int wStart = 0, largest = 0;
Map<Character, Integer> frequencyChar = new HashMap<>();
for (int wEnd = 0; wEnd < arr.length; wEnd++) {
char rightChar = arr[wEnd];
frequencyChar.put(rightChar, wEnd);
if (frequencyChar.size() > 2) {
char leftChar = arr[wStart];
wStart = frequencyChar.get(leftChar) + 1;
frequencyChar.remove(leftChar);
}
largest = Math.max(largest, wEnd - wStart + 1);
}
return largest;
}