Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

1047. Remove All Adjacent Duplicates In String

Easy70.5% Acceptance
StringStack
Asked by:
F
Facebook
Amazon
ProblemHints (1)Solutions (12)VideosCompanies (2)Notes

Problem Statement

You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.

We repeatedly make duplicate removals on s until we no longer can.

Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.

Example 1:

Input: s = "abbaca"
Output: "ca"
Explanation: 
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".

Example 2:

Input: s = "azxxzy"
Output: "ay"

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A

Approach

The key idea in #1047 Remove All Adjacent Duplicates In String is to repeatedly eliminate pairs of identical neighboring characters until no such pair remains. A clean way to simulate this behavior is by using a stack-like approach. As you iterate through the string, compare the current character with the most recent character stored in the stack.

If the top element of the stack matches the current character, it means an adjacent duplicate is found, so you remove the top element. Otherwise, push the current character onto the stack. This mimics the process of continuously removing adjacent duplicates as they appear.

After processing all characters, the stack will contain the final sequence without adjacent duplicates. The result can be constructed by joining the characters in the stack. This approach is efficient because each character is pushed and popped at most once, resulting in O(n) time complexity and O(n) auxiliary space.

Complexity

ApproachTime ComplexitySpace Complexity
Stack-Based SimulationO(n)O(n)
String Builder as StackO(n)O(n)

Video Solution Available

NeetCode

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Use a stack to process everything greedily.

Ready to see the solutions?View Solutions

Solutions (12)

Stack-Based Approach

Use a stack data structure to efficiently remove adjacent duplicates. Traverse through the string, and for each character, check the top of the stack. If the top of the stack is equal to the current character, pop the stack. Otherwise, push the current character onto the stack. Finally, reconstruct the string from the stack.

Time Complexity: O(n), where n is the length of the string since we traverse the string once.
Space Complexity: O(n), as in the worst case, the stack may need to store all characters.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3
4char* removeDuplicates(char* s) {
5    int len = strlen(


Explanation

The solution iteratively checks each character of the string. If the character matches the top of the stack, it removes it (indicating an adjacent duplicate removal). Otherwise, it pushes the character onto the stack. This simulates the removal of adjacent duplicates effectively.

Two-Pointer Approach

This approach also simulates stack behavior but uses a two-pointer technique on the same string to efficiently manage space without using additional data structures. It leverages the properties of strings and index manipulation to override adjacent duplicates.

Time Complexity: O(n), with n being the string length.
Space Complexity: O(1), since it modifies the original string in place.

CC++JavaPythonC#JavaScript
1

Video Solutions

Watch expert explanations and walkthroughs

Remove All Adjacent Duplicates in String II - Leetcode 1209 - Python

NeetCode
13:0553,400 views

Asked By Companies

2 companies
F
Facebook
A
Amazon

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Valid ParenthesesEasy
Longest Valid ParenthesesHard
Simplify PathMedium
Basic CalculatorHard
More similar problems

Related Topics

StringStack

Problem Stats

Acceptance Rate70.5%
DifficultyEasy
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Remove All Adjacent Duplicates In String asked in FAANG interviews?

Yes, variations of this problem are common in technical interviews at major tech companies. Interviewers use it to test understanding of stacks, string processing, and efficient linear-time solutions.

What data structure is best for Remove All Adjacent Duplicates In String?

A stack is the most suitable data structure for this problem because it naturally supports checking and removing the most recent character. In practice, many implementations use a stack or a dynamic string structure like a StringBuilder acting as a stack.

What is the optimal approach for Remove All Adjacent Duplicates In String?

The optimal approach uses a stack to track characters while iterating through the string. If the current character matches the stack's top element, it is removed; otherwise it is added. This ensures each character is processed at most twice, giving O(n) time complexity.

Can Remove All Adjacent Duplicates In String be solved without an explicit stack?

Yes, the problem can be solved using a mutable string structure such as a StringBuilder or array acting like a stack. The logic remains the same: compare the last stored character with the current one and remove or append accordingly.

s
)
;
6
char
*
stack
=
(
char
*
)
malloc
(
len
+
1
)
;
7
int
top
=
-
1
;
8
9
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
10
if
(
top
>=
0
&&
stack
[
top
]
==
s
[
i
]
)
{
11
top
--
;
// Pop the stack
12
}
else
{
13
stack
[
++
top
]
=
s
[
i
]
;
// Push to the stack
14
}
15
}
16
17
stack
[
top
+
1
]
=
'\0'
;
18
return
stack
;
19
}
20
21
int
main
(
)
{
22
char
s
[
]
=
"abbaca"
;
23
printf
(
"%s\n"
,
removeDuplicates
(
s
)
)
;
24
return
0
;
25
}
#
include
<stdio.h>
2
#
include
<string.h>
3
4
char
*
removeDuplicatesTwoPointers
(
char
*
s
)
{
5
int
i
=
0
,
n
=
strlen
(
s
)
;
6
for
(
int
j
=
0
;
j
<
n
;
j
++
,
i
++
)
{
7
s
[
i
]
=
s
[
j
]
;
8
if
(
i
>
0
&&
s
[
i
]
==
s
[
i
-
1
]
)
9
i
-=
2
;
10
}
11
s
[
i
]
=
'\0'
;
12
return
s
;
13
}
14
15
int
main
(
)
{
16
char
s
[
]
=
"abbaca"
;
17
printf
(
"%s\n"
,
removeDuplicatesTwoPointers
(
s
)
)
;
18
return
0
;
19
}

Explanation

The two-pointer method directly modifies the input string array by using `i` as the effective end of the currently processed string. Whenever a duplicate is detected, we backtrack the `i` pointer, simulating a pop operation. This way, space usage is minimized as no extra stack structure is created.