> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Valid Parenthesis String Python Solution

> Tested Python solution for LeetCode 678 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 678, Medium. Topics: String, Dynamic Programming, Stack, Greedy. [View on LeetCode](https://leetcode.com/problems/valid-parenthesis-string/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 678   # by problem number
lcpy gen -s valid_parenthesis_string   # by problem name
```

## Problem

Given a string `s` containing only three types of characters: `'('`, `')'` and `'*'`, return `true` *if* `s` *is **valid***.

The following rules define a **valid** string:

* Any left parenthesis `'('` must have a corresponding right parenthesis `')'`.
* Any right parenthesis `')'` must have a corresponding left parenthesis `'('`.
* Left parenthesis `'('` must go before the corresponding right parenthesis `')'`.
* `'*'` could be treated as a single right parenthesis `')'` or a single left parenthesis `'('` or an empty string `""`.

### Examples

```
Input: s = "()"
Output: true
```

```
Input: s = "(*)"
Output: true
```

```
Input: s = "(*))"
Output: true
```

### Constraints

* 1 \<= s.length \<= 100
* `s[i]` is `'('`, `')'` or `'*'`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/valid_parenthesis_string/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/valid_parenthesis_string/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def check_valid_string(self, s: str) -> bool:
        # Range of possible open-parenthesis counts after processing each char.
        low = 0
        high = 0
        for char in s:
            if char == "(":
                low += 1
                high += 1
            elif char == ")":
                low = max(low - 1, 0)
                high -= 1
            else:  # '*' can act as '(', ')' or empty
                low = max(low - 1, 0)
                high += 1
            if high < 0:
                return False
        return low == 0
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
