Balanced Sequence After Replacement

Given a string of length 'N' containing only the characters: '[', '{', '(', ')', '}', ']'. At certain places, the character 'X' appears in place of any bracket. Your goal is to determine if replacing all the 'X's with suitable brackets can result in a valid balanced sequence.

Example:

Input:
"[X)](X"
"[XX{"
Output:
Valid
Invalid
Explanation:

For the string "[X)](X", replacing the first X with '(' and the second X with ')' results in the sequence "[()]()", which is a valid balanced sequence. However, for the string "[XX{", no replacement will result in a valid bracket sequence.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 20
  • Time limit: 1 sec

Input:

The first line contains an integer 'T' indicating the number of test cases. Each of the following T lines contains a single string to be evaluated.

Output:

For each test case, output "Valid" if a balanced sequence can be achieved after replacing all 'X's, otherwise output "Invalid".

Note:

In the function, return 'True' for a valid string and 'False' for an invalid string without printing.

Be the first one to answer
Add answer anonymously...
Veritas Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter