给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串。有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 )。 任何右括号 ) 必须有相应的左括号 ( 。 左括号 ( 必须在对应的右括号之前 )。 * 可以被视为单个右括号 ) ,或单个左括号 ( ,或一个空字符串。 一个空字符串也被视为有效字符串。
示例1
输入
"()"
输出
true
示例2
输入
"(*)"
输出
true
示例3
输入
"(*))"
输出
true
备注:
字符串大小将在 [1,100] 范围内。
加载中...
import java.util.*; public class Solution { /** * * @param s string字符串 * @return bool布尔型 */ public boolean checkValidString (String s) { // write code here } }
class Solution { public: /** * * @param s string字符串 * @return bool布尔型 */ bool checkValidString(string s) { // write code here } };
# # # @param s string字符串 # @return bool布尔型 # class Solution: def checkValidString(self , s ): # write code here
/** * * @param s string字符串 * @return bool布尔型 */ function checkValidString( s ) { // write code here } module.exports = { checkValidString : checkValidString };
# # # @param s string字符串 # @return bool布尔型 # class Solution: def checkValidString(self , s ): # write code here
package main /** * * @param s string字符串 * @return bool布尔型 */ func checkValidString( s string ) bool { // write code here }
/** * * @param s string字符串 * @return bool布尔型 */ bool checkValidString(char* s ) { // write code here }
"()"
true
"(*)"
true
"(*))"
true