Valid String Problem Statement

Given a string S consisting solely of the characters '(', ')', and '*', determine if it is a valid string.

Definition of Valid String:

1. Every left parenthesis '(' must have a corresponding right parenthesis ')'.
2. Every right parenthesis ')' must have a corresponding left parenthesis '('.
3. A left parenthesis '(' must occur before the corresponding right parenthesis ')'.
4. The character '*' can represent a single left parenthesis '(', a single right parenthesis ')', or an empty string.
5. An empty string is also considered valid.

Objective:

For each provided test case, ascertain whether the string S is valid and print 'Yes' or 'No' as the outcome.

Input:

The first line contains an integer 'T', the number of test cases. Each of the following T lines contains one string 'S'.

Output:

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

Example:

Input:
2
()
(*)))
Output:
Yes
No

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000, where 'N' is the length of string 'S'.
  • Time Limit: 1 sec
AnswerBot
4mo

Determine if a string consisting of '(' , ')' and '*' characters is valid based on given rules.

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

Help your peers!
Select
Add answer anonymously...

SimplifyVMS Software Developer Intern interview questions & answers

A Software Developer Intern was asked 6mo agoQ. Why do you want to join us?
A Software Developer Intern was asked 6mo agoQ. Implement a stack data structure.
A Software Developer Intern was asked 6mo agoQ. Where do you see yourself in the next 5 years?

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 6mo agoQ1. Where do you see yourself in the next 5 years?
A Software Developer Intern was asked 6mo agoQ2. Why do you want to join us?
A Software Developer Intern was asked 6mo agoQ3. Implement a stack data structure.
SimplifyVMS Software Developer Intern 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