Created
November 19, 2023 19:16
-
-
Save optimistiks/242174c0891e88b8e7067c3920f5b751 to your computer and use it in GitHub Desktop.
You are given a string consisting of lowercase English letters. Repeatedly remove adjacent duplicate letters, one pair at a time. Both members of a pair of adjacent duplicate letters need to be removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function removeDuplicates(string) { | |
| const stack = []; | |
| for (const char of string) { | |
| if (stack[stack.length - 1] === char) { | |
| stack.pop(); | |
| } else { | |
| stack.push(char); | |
| } | |
| } | |
| let result = ""; | |
| while (stack.length > 0) { | |
| result = stack.pop() + result; | |
| } | |
| return result; | |
| } | |
| // tc: O(n) | |
| // sc: O(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment