You are viewing a single comment's thread. Return to all comments →
C#
public static int alternate(string s) { // Step 1: Get all unique characters in the string HashSet uniqueChars = new HashSet(s);
int maxLength = 0; // Step 2: Try every pair of unique characters foreach (char char1 in uniqueChars) { foreach (char char2 in uniqueChars) { if (char1 != char2) { int currentLength = 0; char lastChar = '\0'; bool isValid = true; // Step 3: Loop through the string foreach (char c in s) { if (c == char1 || c == char2) { // If two consecutive characters are the same, it's invalid if (c == lastChar) { isValid = false; break; } lastChar = c; currentLength++; } } // If the string is valid, update maxLength if (isValid) { maxLength = Math.Max(maxLength, currentLength); } } } } return maxLength; }
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 →
C#
public static int alternate(string s) { // Step 1: Get all unique characters in the string HashSet uniqueChars = new HashSet(s);