Skip to content

3223. Minimum Length of String After Operations #1136

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to focus on the frequency of each character in the string. Here’s the approach to solve it:

Approach:

  1. Count Character Frequencies:

    • Use a frequency table to count how many times each character appears in the string.
  2. Reduce Characters with Frequency >= 3:

    • If a character appears 3 or more times, we can repeatedly delete two of them until only 2 occurrences are left.
  3. Calculate the Minimum Length:

    • Sum up the remaining counts of all characters after reducing the frequencies.

Let's implement this solution in PHP: 3223. Minimum Length of String After Operations

<?php
/**
 * @param String $s
 * @return Integer
 */
function minimumLength($s) {
    // Step 1: Count the frequenc…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jan 13, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants