Minimum Cost to Make String Valid

Given a string containing only '{' and '}', determine the minimum cost required to make the string valid. A string is considered valid if for every opening bracket '{', there is a corresponding closing bracket '}'. You can convert a '{' into '}' or vice versa, with each conversion costing 1.

Example:

The strings "{}{}", "{{}}", and "{{}{}}" are valid, while "}{}", "{}}{{}", and "{{}}}{" are not.

Example:

Input:
STR = "{{"
Output:
1
Explanation:

Converting the '{' at index 1 (0-based) to '}' results in the string "{}", which is valid.

Constraints:

  • 1 ≤ T ≤ 100
  • 0 ≤ |STR| ≤ 105
  • STR[i] = '{' or '}'
  • Time Limit: 1 sec
Note:
Return -1 if it is impossible to make 'STR' valid.

Input

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

Output

For each test case, output the minimum cost needed to make 'STR' valid. If it is impossible, output -1. Print each result on a new line.
AnswerBot
4mo

Given a string with only '{' and '}', find the minimum cost to make it valid by converting brackets.

  • Iterate through the string and keep track of the opening and closing brackets

  • If there are more closi...read more

Help your peers!
Select
Add answer anonymously...

Adobe Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Unbounded Knapsack Problem Statement Given ‘N’ items, each with a specific profi...read more
A Software Developer Intern was asked Q. Minimum Cost to Make String Valid Given a string containing only '{' and '}', de...read more
A Software Developer Intern was asked Q. Digit Count In Range Problem Statement Given an integer K, and two numbers A and...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Unbounded Knapsack Problem Statement Given ‘N’ items, each with a specific profi...read more
A Software Developer Intern was asked Q2. Minimum Cost to Make String Valid Given a string containing only '{' and '}', de...read more
A Software Developer Intern was asked Q3. Digit Count In Range Problem Statement Given an integer K, and two numbers A and...read more
Adobe 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