diff --git a/solution/3500-3599/3561.Resulting String After Adjacent Removals/README.md b/solution/3500-3599/3561.Resulting String After Adjacent Removals/README.md index 578ccc38db9d4..f9c935f73907e 100644 --- a/solution/3500-3599/3561.Resulting String After Adjacent Removals/README.md +++ b/solution/3500-3599/3561.Resulting String After Adjacent Removals/README.md @@ -90,32 +90,115 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3561.Re -### 方法一 +### 方法一:栈 + +我们可以使用栈来模拟移除相邻字符的过程。遍历字符串中的每个字符,如果栈顶字符与当前字符是连续的(即它们的 ASCII 值差为 1 或 25),则将栈顶字符弹出;否则,将当前字符压入栈中。最后,栈中的字符就是无法再移除的结果,我们将栈中的字符连接成字符串并返回。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是字符串的长度。 #### Python3 ```python - +class Solution: + def resultingString(self, s: str) -> str: + stk = [] + for c in s: + if stk and abs(ord(c) - ord(stk[-1])) in (1, 25): + stk.pop() + else: + stk.append(c) + return "".join(stk) ``` #### Java ```java - +class Solution { + public String resultingString(String s) { + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (stk.length() > 0 && isContiguous(stk.charAt(stk.length() - 1), c)) { + stk.deleteCharAt(stk.length() - 1); + } else { + stk.append(c); + } + } + return stk.toString(); + } + + private boolean isContiguous(char a, char b) { + int t = Math.abs(a - b); + return t == 1 || t == 25; + } +} ``` #### C++ ```cpp - +class Solution { +public: + string resultingString(string s) { + string stk; + for (char c : s) { + if (stk.size() && (abs(stk.back() - c) == 1 || abs(stk.back() - c) == 25)) { + stk.pop_back(); + } else { + stk.push_back(c); + } + } + return stk; + } +}; ``` #### Go ```go +func resultingString(s string) string { + isContiguous := func(a, b rune) bool { + x := abs(int(a - b)) + return x == 1 || x == 25 + } + stk := []rune{} + for _, c := range s { + if len(stk) > 0 && isContiguous(stk[len(stk)-1], c) { + stk = stk[:len(stk)-1] + } else { + stk = append(stk, c) + } + } + return string(stk) +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +#### TypeScript + +```ts +function resultingString(s: string): string { + const stk: string[] = []; + const isContiguous = (a: string, b: string): boolean => { + const x = Math.abs(a.charCodeAt(0) - b.charCodeAt(0)); + return x === 1 || x === 25; + }; + for (const c of s) { + if (stk.length && isContiguous(stk.at(-1)!, c)) { + stk.pop(); + } else { + stk.push(c); + } + } + return stk.join(''); +} ``` diff --git a/solution/3500-3599/3561.Resulting String After Adjacent Removals/README_EN.md b/solution/3500-3599/3561.Resulting String After Adjacent Removals/README_EN.md index 342dd973740fd..1d88885331f2c 100644 --- a/solution/3500-3599/3561.Resulting String After Adjacent Removals/README_EN.md +++ b/solution/3500-3599/3561.Resulting String After Adjacent Removals/README_EN.md @@ -88,32 +88,115 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3561.Re -### Solution 1 +### Solution 1: Stack + +We can use a stack to simulate the process of removing adjacent characters. Iterate through each character in the string. If the character at the top of the stack and the current character are consecutive (i.e., their ASCII values differ by 1 or 25), pop the top character from the stack; otherwise, push the current character onto the stack. Finally, the characters remaining in the stack are those that can no longer be removed. Join the characters in the stack into a string and return it. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string. #### Python3 ```python - +class Solution: + def resultingString(self, s: str) -> str: + stk = [] + for c in s: + if stk and abs(ord(c) - ord(stk[-1])) in (1, 25): + stk.pop() + else: + stk.append(c) + return "".join(stk) ``` #### Java ```java - +class Solution { + public String resultingString(String s) { + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (stk.length() > 0 && isContiguous(stk.charAt(stk.length() - 1), c)) { + stk.deleteCharAt(stk.length() - 1); + } else { + stk.append(c); + } + } + return stk.toString(); + } + + private boolean isContiguous(char a, char b) { + int t = Math.abs(a - b); + return t == 1 || t == 25; + } +} ``` #### C++ ```cpp - +class Solution { +public: + string resultingString(string s) { + string stk; + for (char c : s) { + if (stk.size() && (abs(stk.back() - c) == 1 || abs(stk.back() - c) == 25)) { + stk.pop_back(); + } else { + stk.push_back(c); + } + } + return stk; + } +}; ``` #### Go ```go +func resultingString(s string) string { + isContiguous := func(a, b rune) bool { + x := abs(int(a - b)) + return x == 1 || x == 25 + } + stk := []rune{} + for _, c := range s { + if len(stk) > 0 && isContiguous(stk[len(stk)-1], c) { + stk = stk[:len(stk)-1] + } else { + stk = append(stk, c) + } + } + return string(stk) +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +#### TypeScript + +```ts +function resultingString(s: string): string { + const stk: string[] = []; + const isContiguous = (a: string, b: string): boolean => { + const x = Math.abs(a.charCodeAt(0) - b.charCodeAt(0)); + return x === 1 || x === 25; + }; + for (const c of s) { + if (stk.length && isContiguous(stk.at(-1)!, c)) { + stk.pop(); + } else { + stk.push(c); + } + } + return stk.join(''); +} ``` diff --git a/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.cpp b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.cpp new file mode 100644 index 0000000000000..48831cb9da038 --- /dev/null +++ b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + string resultingString(string s) { + string stk; + for (char c : s) { + if (stk.size() && (abs(stk.back() - c) == 1 || abs(stk.back() - c) == 25)) { + stk.pop_back(); + } else { + stk.push_back(c); + } + } + return stk; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.go b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.go new file mode 100644 index 0000000000000..c7d4fa740df87 --- /dev/null +++ b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.go @@ -0,0 +1,22 @@ +func resultingString(s string) string { + isContiguous := func(a, b rune) bool { + x := abs(int(a - b)) + return x == 1 || x == 25 + } + stk := []rune{} + for _, c := range s { + if len(stk) > 0 && isContiguous(stk[len(stk)-1], c) { + stk = stk[:len(stk)-1] + } else { + stk = append(stk, c) + } + } + return string(stk) +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} \ No newline at end of file diff --git a/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.java b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.java new file mode 100644 index 0000000000000..cfa0fbd8b055d --- /dev/null +++ b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public String resultingString(String s) { + StringBuilder stk = new StringBuilder(); + for (char c : s.toCharArray()) { + if (stk.length() > 0 && isContiguous(stk.charAt(stk.length() - 1), c)) { + stk.deleteCharAt(stk.length() - 1); + } else { + stk.append(c); + } + } + return stk.toString(); + } + + private boolean isContiguous(char a, char b) { + int t = Math.abs(a - b); + return t == 1 || t == 25; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.py b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.py new file mode 100644 index 0000000000000..8a915c0527184 --- /dev/null +++ b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def resultingString(self, s: str) -> str: + stk = [] + for c in s: + if stk and abs(ord(c) - ord(stk[-1])) in (1, 25): + stk.pop() + else: + stk.append(c) + return "".join(stk) diff --git a/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.ts b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.ts new file mode 100644 index 0000000000000..4423b269ab144 --- /dev/null +++ b/solution/3500-3599/3561.Resulting String After Adjacent Removals/Solution.ts @@ -0,0 +1,15 @@ +function resultingString(s: string): string { + const stk: string[] = []; + const isContiguous = (a: string, b: string): boolean => { + const x = Math.abs(a.charCodeAt(0) - b.charCodeAt(0)); + return x === 1 || x === 25; + }; + for (const c of s) { + if (stk.length && isContiguous(stk.at(-1)!, c)) { + stk.pop(); + } else { + stk.push(c); + } + } + return stk.join(''); +}