3561. Resulting String After Adjacent Removals LeetCode Solution
In this guide, you will get 3561. Resulting String After Adjacent Removals LeetCode Solution with the best time and space complexity. The solution to Resulting String After Adjacent Removals problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Resulting String After Adjacent Removals solution in C++
- Resulting String After Adjacent Removals solution in Java
- Resulting String After Adjacent Removals solution in Python
- Additional Resources
Problem Statement of Resulting String After Adjacent Removals
You are given a string s consisting of lowercase English letters.
You must repeatedly perform the following operation while the string s has at least two consecutive characters:
Remove the leftmost pair of adjacent characters in the string that are consecutive in the alphabet, in either order (e.g., ‘a’ and ‘b’, or ‘b’ and ‘a’).
Shift the remaining characters to the left to fill the gap.
Return the resulting string after no more operations can be performed.
Note: Consider the alphabet as circular, thus ‘a’ and ‘z’ are consecutive.
Example 1:
Input: s = “abc”
Output: “c”
Explanation:
Remove “ab” from the string, leaving “c” as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is “c”.
Example 2:
Input: s = “adcb”
Output: “”
Explanation:
Remove “dc” from the string, leaving “ab” as the remaining string.
Remove “ab” from the string, leaving “” as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is “”.
Example 3:
Input: s = “zadb”
Output: “db”
Explanation:
Remove “za” from the string, leaving “db” as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is “db”.
Constraints:
1 <= s.length <= 105
s consists only of lowercase English letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
3561. Resulting String After Adjacent Removals LeetCode Solution in C++
class Solution {
public:
string resultingString(string s) {
string ans;
for (const char c : s)
if (!ans.empty() &&
(abs(ans.back() - c) == 1 || abs(ans.back() - c) == 25))
ans.pop_back();
else
ans += c;
return ans;
}
};
/* code provided by PROGIEZ */
3561. Resulting String After Adjacent Removals LeetCode Solution in Java
class Solution {
public String resultingString(String s) {
StringBuilder sb = new StringBuilder();
for (final char c : s.toCharArray())
if (sb.length() > 0 && (Math.abs(sb.charAt(sb.length() - 1) - c) == 1 ||
Math.abs(sb.charAt(sb.length() - 1) - c) == 25))
sb.deleteCharAt(sb.length() - 1);
else
sb.append(c);
return sb.toString();
}
}
// code provided by PROGIEZ
3561. Resulting String After Adjacent Removals LeetCode Solution in Python
class Solution:
def resultingString(self, s: str) -> str:
stack = []
for c in s:
if stack and abs(ord(stack[-1]) - ord(c)) in (1, 25):
stack.pop()
else:
stack.append(c)
return ''.join(stack)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.