package com.nuo.Y_21M_12;
public class day10 {
public static void main(String[] args) {
System.out.println(Solution10.checkValidString("(*))"));
}
}
class Solution10 {
public static boolean checkValidString(String s) {
int left = 0;
int right = 0;
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i++) {
left = c[i] == ')'? left - 1 : left + 1;
right = c[c.length - i - 1] == '(' ? right - 1 : right + 1;
if (left < 0|| right < 0){
return false;
}
}
return true;
}
}
有去参考别人的题解 ,利用了贪心算法,大致思路是从左往右和从右往左分别进行判断,从左往右时,因为 '*'可以充当 '(' & ')' ,为了确保'('数量足够 ,把'*'视为'(',每进行一次for循环,判断')'是否有足够的'('去匹配,若没有,则返回 false,否则循环继续,从右往左同理。从左往右和从右往左分别进行判断去确保'(' 和 ')'可以完全配对
PS :该好好去学下贪心算法了......



