Skip to main content

JP Morgan

JP Morgan OA-6 2024

Problem Description

You are given a string s that consists only of the characters 'X' and 'Y'. In one move, you can delete either the substring 'XY' or 'YY' and concatenate the remaining parts of the string.

Objective:

Return the minimum possible length of the string after performing any number of moves.

Note:

  • A substring is a contiguous subsequence of a string.
  • You are allowed to perform any number of moves until no more 'XY' or 'YY' can be deleted.

Constraints:

1 <= s.length <= 10^5


Example:

Input:

s = "YXYYX"

Output:

1

Explanation:

  1. Remove 'XY' from "YXYYX" → "YYX"
  2. Remove 'YY' from "YYX" → "X"
  3. The remaining string is "X", which has a length of 1.