当前位置:网站首页>16 -- remove invalid parentheses
16 -- remove invalid parentheses
2022-06-27 15:34:00 【JH_ Cao】
subject
- Here's a string of parentheses and letters s , Remove the minimum number of invalid parentheses , Make the input string valid .
Returns all possible results . You can press In any order return .
Example 1:
Input :s = “()())()”
Output :[“(())()”,“()()()”]
Example 2:
Input :s = “(a)())()”
Output :[“(a())()”,“(a)()()”]
Example 3:
Input :s = “)(”
Output :[“”]
- Ideas
- Find out first "(“ The number of extra left and ”)" The number of extra right
- Delete one by one , For example, there are two more "(“, One ”(“, In all cases , It can only be deleted one by one , First delete the first ”(“, Continue recursion , Delete the second ”(“, Re recursion , Delete ”)", When left and right be equal to 0, And the string meets the requirements , Just add it to the answer .
The code is as follows
//
// facebook04.swift
// ForDreams
//
// Created by cao hua on 2022/6/25.
//
import Foundation
class facebook04 {
//s = "()())()"
//s = "(')'())()"
// "()()')'()"
var res = [String]()
func removeInvalidParentheses(_ s: String) -> [String] {
// 1. Traverse the number of left and right parentheses
let left = getLRCount(s).first! // 0
let right = getLRCount(s).last! // 1
//
helper(s, 0, left, right)
return res
}
private func getLRCount(_ s: String) -> [Int] {
var left = 0
var right = 0
for c in s {
if String(c) == "(" {
left += 1
} else if String(c) == ")" {
if left == 0 {
right += 1
} else {
left -= 1
}
}
}
return [left, right]
}
private func helper(_ s: String, _ start: Int, _ left: Int, _ right: Int) {
if left == 0 && right == 0 {
if isValid(s) {
res.append(s)
}
return
}
for i in start..<s.count {
if i != start && s.charAt(i) == s.charAt(i - 1) {
continue
}
if left + right > s.count - i { // ")))" -- No more elements to be added
return
}
if left > 0 && s.charAt(i) == "(" {
let tempArr = Array(s)
let pre = String(tempArr[0..<i])
let post = String(tempArr[(i+1)..<s.count])
let tempStr = pre + post
helper(tempStr, i, left - 1, right)
}
if right > 0 && s.charAt(i) == ")" {
let tempArr = Array(s)
let pre = String(tempArr[0..<i])
let post = String(tempArr[(i+1)..<s.count])
let tempStr = pre + post
helper(tempStr, i, left, right - 1)
}
}
}
private func isValid(_ s: String) -> Bool {
var cnt = 0
for c in s {
if String(c) == "(" {
cnt += 1
} else if String(c) == ")" {
cnt -= 1
if cnt < 0 {
return false
}
}
}
return cnt == 0
}
}
extension String {
func charAt(_ index: Int) -> Character {
let tempArr = Array(self)
return tempArr[index]
}
}
边栏推荐
- E modulenotfounderror: no module named 'psychopg2' (resolved)
- Design of UART controller based on FPGA (with code)
- QT notes (XXVIII) using qwebengineview to display web pages
- 洛谷入门2【分支结构】题单题解
- Professor huangxutao, a great master in CV field, was born at the age of 86. UIUC specially set up a doctoral scholarship to encourage cutting-edge students
- 关于快速幂
- Knightctf 2022 web section
- Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
- 【170】PostgreSQL 10字段类型从字符串修改成整型,报错column cannot be cast automatically to type integer
- Different perspectives
猜你喜欢

Interview question: rendering 100000 data solutions

Keep valid digits; Keep n digits after the decimal point;

Teach you how to package and release the mofish Library

Unity3d best practices: folder structure and source control

Lei Jun lost another great general, and liweixing, the founding employee of Xiaomi No. 12, left his post. He once had porridge to create Xiaomi; Intel's $5.4 billion acquisition of tower semiconductor

Design and implementation of food recipe and ingredients website based on vue+node+mysql

Volatile and JMM

What is the London Silver unit

【kotlin】第二天

Creation and use of static library (win10+vs2022
随机推荐
CAS comparison and exchange
[digital signal processing] discrete time signal (discrete time signal knowledge points | signal definition | signal classification | classification according to certainty | classification according t
专家:让你低分上好校的都是诈骗
Abnormal analysis of pcf8591 voltage measurement data
PR second training notes
Vscode uses yapf auto format to set the maximum number of characters per line
Synchronized and lock escalation
Lei Jun lost another great general, and liweixing, the founding employee of Xiaomi No. 12, left his post. He once had porridge to create Xiaomi; Intel's $5.4 billion acquisition of tower semiconductor
sql注入原理
专用发票和普通发票的区别
Elegant custom ThreadPoolExecutor thread pool
SQL parsing practice of Pisa proxy
R language triple becomes matrix matrix becomes triple
AI begets the moon, and thousands of miles share the literary heart
Pycharm安装与设置
Teach you how to package and release the mofish Library
2022-2-15 learning the imitated Niuke project - Section 5 shows comments
AQS Abstract queue synchronizer
Can the teacher tell me what the fixed income + products are mainly invested in?
Pri3d: a representation learning method for 3D scene perception using inherent attributes of rgb-d data