Min Moves to Valid Parentheses: LeetCode 921 Solution

Day 42 of Daily DSA 🚀 Solved LeetCode 921: Minimum Add to Make Parentheses Valid ✅ Problem: Given a parentheses string s, return the minimum number of moves required to make it valid. Rules: A valid string has balanced opening and closing brackets You can insert a parenthesis at any position Return the minimum additions needed Approach: Used a Stack + Counter approach to track unmatched parentheses. Steps: Traverse the string Push '(' onto stack If ')' and stack has '(' → pop Else → increment counter (unmatched closing) At the end → remaining stack size = unmatched openings Result = unmatched openings + unmatched closings ⏱ Complexity: • Time: O(n) • Space: O(n) 📊 LeetCode Stats: • Runtime: 1 ms (Beats 67.19%) ⚡ • Memory: 43.08 MB Nice extension of the Valid Parentheses problem, focusing on fixing invalid cases instead of just checking them. #DSA #LeetCode #Java #Stack #ProblemSolving #CodingJourney #Consistency

  • text

To view or add a comment, sign in

Explore content categories