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
#
andx
in the stringS
. - If there are more
#
characters thanx
characters, output a positive integer equal to the difference (#
-x
). This represents the minimum number ofx
characters needed to balance the string. - If there are more
x
characters 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
S
containing only the characters#
andx
.
Output Format
Print an integer:
- A positive integer if more
#
characters are present (representing the minimumx
characters needed). - A negative integer if more
x
characters are present (representing the minimum#
characters needed). 0
if 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 2x
characters. - To balance, we need 1 more
x
, so the output is1
.