TCS OA-29 2024
Problem Description
You are given a string S consisting of the characters # and x. The length of the string is variable. Your task is to determine the minimum number of characters (# or x) required to make the string "valid."
The string is considered valid if the number of # and x characters are equal.
Requirements
- Count the occurrences of
#andxin the stringS. - If there are more
#characters thanxcharacters, output a positive integer equal to the difference (#-x). This represents the minimum number ofxcharacters needed to balance the string. - If there are more
xcharacters than#characters, output a negative integer equal to the difference (#-x). This represents the minimum number of#characters needed to balance the string. - If the counts are equal, output
0, indicating the string is already valid.
Input Format
- The first line contains a string
Scontaining only the characters#andx.
Output Format
Print an integer:
- A positive integer if more
#characters are present (representing the minimumxcharacters needed). - A negative integer if more
xcharacters are present (representing the minimum#characters needed). 0if the string is already balanced.
Constraints
- 1 ≤ arr.size() ≤ 10⁴
- -10³ ≤ arr[i] ≤ 10³
- -10⁷ ≤ k ≤ 10⁷
Example
Sample Input:
"##x#xx"
Sample Output:
1
Explanation:
- There are 3
#characters and 2xcharacters. - To balance, we need 1 more
x, so the output is1.