Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static string SuperReducedString(string s)
- {
- // Convert the string to a character array for easier manipulation
- char[] chars = s.ToCharArray();
- int length = chars.Length;
- // Loop through the characters, checking for adjacent duplicates
- for (int i = 0; i < length - 1; i++)
- {
- // If adjacent characters are the same, remove them and adjust the array
- if (chars[i] == chars[i + 1])
- {
- // Shift the characters after the adjacent duplicates to the left
- for (int j = i + 2; j < length; j++)
- {
- chars[j - 2] = chars[j];
- }
- // Adjust the length and reset the index
- length -= 2;
- i = -1; // Start over after removing adjacent duplicates
- }
- }
- // If the final string is empty, return "Empty String", otherwise return the reduced string
- if (length == 0)
- return "Empty String";
- else
- return new string(chars, 0, length);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement