Skip to content

Instantly share code, notes, and snippets.

@optimistiks
Created November 19, 2023 19:16
Show Gist options
  • Select an option

  • Save optimistiks/242174c0891e88b8e7067c3920f5b751 to your computer and use it in GitHub Desktop.

Select an option

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.
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