All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0601_0700.s0678_valid_parenthesis_string.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0601_0700.s0678_valid_parenthesis_string;

// #Medium #String #Dynamic_Programming #Greedy #Stack

public class Solution {
    public boolean checkValidString(String s) {
        int lo = 0;
        int hi = 0;
        for (int i = 0; i < s.length(); i++) {
            lo += s.charAt(i) == '(' ? 1 : -1;
            hi += s.charAt(i) != ')' ? 1 : -1;
            if (hi < 0) {
                break;
            }
            lo = Math.max(0, lo);
        }
        return lo == 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy