Created
February 5, 2020 17:17
-
-
Save fengyitsai/e7a3bb52eacc292d5581dd1ef7042826 to your computer and use it in GitHub Desktop.
1249. Minimum Remove to Make Valid Parentheses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func minRemoveToMakeValid(_ s: String) -> String { | |
var array = s.map { String($0) } | |
var leftBraces = [Int]() | |
for i in 0..<array.count { | |
if array[i] == "(" { | |
leftBraces.append(i) | |
continue | |
} | |
if array[i] == ")" { | |
if leftBraces.isEmpty { | |
array[i] = "" | |
} else { | |
leftBraces.removeLast() | |
} | |
} | |
} | |
for i in leftBraces { | |
array[i] = "" | |
} | |
return array.joined() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment