Skip to content

Instantly share code, notes, and snippets.

@fengyitsai
Created February 5, 2020 17:17
Show Gist options
  • Save fengyitsai/e7a3bb52eacc292d5581dd1ef7042826 to your computer and use it in GitHub Desktop.
Save fengyitsai/e7a3bb52eacc292d5581dd1ef7042826 to your computer and use it in GitHub Desktop.
1249. Minimum Remove to Make Valid Parentheses
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