Remove Adjacent Duplicates in String with Stack

Day 49 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Remove All Adjacent Duplicates in String We were given a string s. Task was to remove all adjacent duplicates. If duplicates are removed, new duplicates may form. So we repeat until no duplicates remain. Example: "abbaca" → "ca" 💻 Approach (Using Stack) 🔹️Create an empty stack. 🔹️Traverse each character. 🔹️If stack is not empty and top == current character → pop 🔹️Else → push current character 🔹️At the end, build string from stack Duplicates get removed automatically. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Stack helps in handling adjacent comparisons. ▫️Removing elements can create new patterns. ▫️Single pass solution is possible with stack. ▫️String problems often map to stack operations. Day 49 completed. Almost at 50 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic

To view or add a comment, sign in

Explore content categories