1750. Minimum Length of String After Deleting Similar Ends

1750. Minimum Length of String After Deleting Similar Ends

Problem Level : Medium

Topics :  Two Pointers  String

Problem Description : 1750. Minimum Length of String After Deleting Similar Ends

Given a string s consisting only of characters 'a''b', and 'c'. You are asked to apply the following algorithm on the string any number of times:

  1. Pick a non-empty prefix from the string s where all the characters in the prefix are equal.
  2. Pick a non-empty suffix from the string s where all the characters in this suffix are equal.
  3. The prefix and the suffix should not intersect at any index.
  4. The characters from the prefix and suffix must be the same.
  5. Delete both the prefix and the suffix.

Return the minimum length of s after performing the above operation any number of times (possibly zero times).

 

Example 1:

Input: s = "ca"
Output: 2
Explanation: You can't remove any characters, so the string stays as is.

Example 2:

Input: s = "cabaabac"
Output: 0
Explanation: An optimal sequence of operations is:
- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
- Take prefix = "a" and suffix = "a" and remove them, s = "baab".
- Take prefix = "b" and suffix = "b" and remove them, s = "aa".
- Take prefix = "a" and suffix = "a" and remove them, s = "".

Example 3:

Input: s = "aabccabba"
Output: 3
Explanation: An optimal sequence of operations is:
- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".

 

Constraints:

  • 1 <= s.length <= 105
  • s only consists of characters 'a''b', and 'c'.
Solution :

       Approach:

  1. The function minimumLength takes a string s as input and returns an integer representing the minimum length of the string after performing a certain operation.

  2. Two pointers, i and j, are initialized to the beginning and end of the string, respectively.

  3. The code uses a while loop with the condition i < j to iterate until the pointers meet or cross each other.

  4. Inside the loop, it checks if the characters at positions i and j are equal. If they are, it enters a nested loop to find the prefix and suffix of the same character.

  5. The variable c is assigned the character at position i, which represents the common character for the prefix and suffix.

  6. The inner while loop increments the pointer i as long as the characters at position i are equal to c, effectively finding the prefix.

  7. Similarly, the second inner while loop decrements the pointer j as long as the characters at position j are equal to c, finding the suffix.

  8. The outer while loop continues until the pointers i and j no longer point to equal characters.

  9. Finally, the function returns the minimum length of the remaining string, which is calculated as j - i + 1. This accounts for the characters between the last found prefix and suffix.

Code :

Output :

Input:   s = “aabccabba”

Output:  3

WhatsApp Group Join Now
Telegram Group Join Now
Instagram Group Join Now
Linkedin Page Join Now

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top