// 时间复杂度 O(n)
// 空间复杂度 O(n)
export default function checkCharacter(str:string):boolean {
const arr = []
const leftCharacter = ["{","[","("]
const rightCharacter = ["}","]",")"]
if(!str){
return true
}
for (let item of str) {
if(leftCharacter.indexOf(item)>-1){
arr.push(item)
}else if(rightCharacter.indexOf(item)>-1){
if(leftCharacter.indexOf(arr[arr.length-1]) === rightCharacter.indexOf(item)){
arr.pop()
}else {
return false
}
}
}
return arr.length===0
}
12-30
2439
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交