Validate Parentheses with Optional Wildcard Characters

Quick Overview

Validate Parentheses with Optional Wildcard Characters. Return whether s can become a balanced parenthesis string by independently treating each * as (, ), or the empty string.

Validate Parentheses with Optional Wildcard Characters

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return whether s can become a balanced parenthesis string by independently treating each * as (, ), or the empty string. Every closing parenthesis must match an earlier opening one; all openings must eventually close. ```cpp class Solution { public: bool checkValidString(string s); }; ``` ### Examples **Example 1:** ```text s = "**()" Output: true Explanation: Treat both stars as empty. ``` **Example 2:** ```text s = ")*" Output: false Explanation: No later star can supply an earlier opening parenthesis. ``` ### Constraints - The input has 1..100 characters; its alphabet is exactly (, ), and *. ### Clarifying Questions Preserve prefix validity and all three possible star meanings. ```hint Read the boundary condition Can the range of possible unmatched-opening counts be summarized without enumerating every star assignment? ``` ### Contract considerations Preserve prefix validity and all three possible star meanings.

Overview: Validate Parentheses with Optional Wildcard Characters. Return whether s can become a balanced parenthesis string by independently treating each * as (, ), or the empty string.

Google logo
Google
Sep 10, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return whether s can become a balanced parenthesis string by independently treating each * as (, ), or the empty string. Every closing parenthesis must match an earlier opening one; all openings must eventually close.

class Solution {
public:
    bool checkValidString(string s);
};

Examples

Example 1:

s = "**()"
Output: true
Explanation: Treat both stars as empty.

Example 2:

s = ")*"
Output: false
Explanation: No later star can supply an earlier opening parenthesis.

Constraints

  • The input has 1..100 characters; its alphabet is exactly (, ), and *.

Clarifying Questions Guidance

Preserve prefix validity and all three possible star meanings.

Contract considerations

Preserve prefix validity and all three possible star meanings.