Skip to main content

TCS

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

  1. Count the occurrences of # and x in the string S.
  2. If there are more # characters than x characters, output a positive integer equal to the difference (# - x). This represents the minimum number of x characters needed to balance the string.
  3. 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.
  4. 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 # and x.

Output Format

Print an integer:

  • A positive integer if more # characters are present (representing the minimum x 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 2 x characters.
  • To balance, we need 1 more x, so the output is 1.