My first approach I thought of was brute force. The first step was to pick a letter from the string and check its neighbors. For each character, I’d check whether the same character exists both before and after its current position. Next, I will use a list to track the previous and updated versions of the string.
Finally I update the string dynamically. This involves removing the required characters from the string and adjusting the indices to reflect the changes. Then I’d repeat the process for the remaining characters, continuing until no further operations are possible. This approach could have worked (might run into TLE - O(n^2)) but I thought of something better (I think).