Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Using Variables, Input/Output, Methods, Arithmetic
```def balancedpara(result, opencount, open, close)
if open < opencount
balancedpara(result + "(", opencount, open + 1, close)
end
if open > close
balancedpara(result + ")", opencount, open, close + 1)
end
if close == opencount
puts "[" + result + "]"
return
end
end
def main()
puts "enter parantheses"
inp = gets.chomp
splited = inp.split('')
open = 0
close = 0
opencount = splited.count('(')
closecount = splited.count(')')
#result = ""
balancedpara("", opencount, 0, 0)
end
main()```
Explanation
Here we want to take input from user with parentheses and containing letter. Remove all invalid parentheses in order to make it valid, are return all possible outputs from it.
Code explanation
Here it count total number of open parentheses and based on that it perform other operations.But that is not the right way to do. Expected output is given below.
Expected Output
"Note: The input string may contain letters other than the parentheses ( and ). Example 1:
Input: ""()())()"" Output: [""()()()"", ""(())()""]
Example 2:
Input: ""(a)())()"" Output: [""(a)()()"", ""(a())()""]
Example 3:
Input: "")("" Output: [""""]"
Aucun commentaire:
Enregistrer un commentaire