You are viewing a single comment's thread. Return to all comments →
Java 33/33 test cases, O(n^2)
public static int alternate(String s) { if (s.length() == 1) { return 0; } int maxAlternateLength = 0; for (char c1 = 'a'; c1 <= 'z'; c1++) { for (char c2 = (char) (c1 + 1); c2 <= 'z'; c2++) { int alternateLength = 0; char prev = '\0'; boolean isValid = true; for (char ch : s.toCharArray()) { if (ch == c1 || ch == c2) { if (ch == prev) { isValid = false; break; } alternateLength++; prev = ch; } } if (isValid) { maxAlternateLength = Math.max(maxAlternateLength, alternateLength); } } } return maxAlternateLength; }
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
Java 33/33 test cases, O(n^2)