Valid String Problem Statement

You are provided with a string S containing only three types of characters: ('), )'), and *.

Explanation:

A Valid String is characterized by the following conditions:

1. Each left parenthesis '(' must have a corresponding right parenthesis ')'.
2. Each right parenthesis ')' must have a corresponding left parenthesis '('.
3. A left parenthesis '(' must appear before its corresponding right parenthesis ')'.
4. The asterisk '*' can be considered as either a right parenthesis ')', a left parenthesis '(', or an empty string.
5. An empty string is considered valid.

Your task is to determine whether the given string is a Valid String.

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case consists of a single line with the string 'S'.

Output:

For each test case, output 'Yes' if the string 'S' is valid; otherwise, output 'No'. Each result should be printed on a new line.

Example:

Input:
T = 2
S = "()"
S = "(*)"
Output:
Yes
Yes

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • Where N is the length of the string S.
Note:
You need to implement the function only, the I/O is handled already.
AnswerBot
4mo

Check if a given string containing parentheses and asterisks is valid based on certain conditions.

  • Iterate through the string and keep track of the count of left parentheses, right parentheses, and ast...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Visa

Q. How would you design Google Pay?
Q. HLD of recursive
Q. Why did you only pass 10 out of 14 test cases in the 4th question?
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits