1047. Remove All Adjacent Duplicates In String
Problem Solving - Day 40
Hello, reader ๐๐ฝ ! Welcome to day 40 of the series on Problem Solving. Through this series, I aim to pick up at least one question everyday and share my approach for solving it.
Today, I will be picking up LeetCode's daily challenge problem: 1047. Remove All Adjacent Duplicates In String.
๐ค Problem Statement
- Given a string s consisting of lowercase English letters, return the string after removing the adjacent duplicates.
- A duplicate removal consists of choosing two adjacent and equal letters and removing them.
Repeatedly make duplicate removals on s until we no longer can.
E.g.:
s => "abbaca"
=>"ca"
s => "azxxzy"
=>"ay"
๐ฌ Thought Process - In-Place Removal using StringBuilder
- One way to handle removing the duplicates is using StringBuilder in Java. This facilitates duplicate removal in place as well.
- Initially, the StringBuilder will be empty string
- We begin the iteration of the input string and keep checking the current character with the last character (if it exists) of the string builder.
- Whenever there's a match between adjacent characters, we delete the last character from the StringBuilder.
- But, if there's no match then we continue with the iteration until penultimate character.
๐ฉ๐ฝโ๐ป Solution - In-Place Removal using StringBuilder
class Solution {
public String removeDuplicates(String s) {
class Solution {
public String removeDuplicates(String s) {
if(s == null || s.length() == 0) {
return "";
}
int n = s.length();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
char ch = s.charAt(i);
int m = sb.length();
if(m > 0 && ch == sb.charAt(m-1)) {
sb.delete(m-1, m);
}
else {
sb.append(ch);
}
}
return sb.toString();
}
}
Time Complexity: O(n)
- n = length of string
Space Complexity: O(n)
- n = length of string
๐ฌ Thought Process - Stack
- We can use a stack that pushes characters from the string.
- We iterate through the string and whenever we find a match with the top most character of the stack, we pop it.
- This process is repeated until the end of string.
- Finally, we iterate through the stack and append. the characters to the output string.
๐ฉ๐ฝโ๐ป Solution - Stack
class Solution {
public String removeDuplicates(String s) {
if(s == null || s.length() == 0) {
return "";
}
int n = s.length();
Stack<Character> stack = new Stack<>();
for(int i = 0; i<n; i++) {
char ch = s.charAt(i);
if(!stack.isEmpty()) {
char top = stack.peek();
if(top == ch) {
stack.pop();
continue;
}
}
stack.push(ch);
}
StringBuilder sb = new StringBuilder();
for(char ch: stack) {
sb.append(ch);
}
return sb.toString();
}
}
Time Complexity: O(n)
- n = length of string Space Complexity: O(n)
- n = length of string
- We use extra space to maintain stack and the string builder ```
- You can find the link to the GitHub repo for this question here: 1047. Remove All Adjacent Duplicates In String.
Conclusion
That's a wrap for today's problem. If you liked my explanation then please do drop a like/ comment. Also, please correct me if I've made any mistakes or if you want me to improve something!
Thank you for reading!