Post

3455 Minimum Length Of String After Operations

3455 Minimum Length Of String After Operations

Minimum Length of String After Operations image

You are given a string s.

You can perform the following process on s any number of times:

1
2
3
Choose an index i in the string such that there is **at least** one character to the left of index i that is equal to s[i], and **at least** one character to the right that is also equal to s[i].
Delete the **closest** character to the **left** of index i that is equal to s[i].
Delete the **closest** character to the **right** of index i that is equal to s[i].

Return the minimum length of the final string s that you can achieve.

 

Example 1:

Input: s = “abaacbcbb”

Output: 5

Explanation:

We do the following operations:

1
2
Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = "bacbcbb".
Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = "acbcb".

Example 2:

Input: s = “aa”

Output: 2

Explanation:

We cannot perform any operations, so we return the length of the original string.

 

Constraints:

1
2
1 <= s.length <= 2 * 105
s consists only of lowercase English letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

func minimumLength(s string) int {
    // if there are 3 chars then 2 can be removed 1 is left 
    // if there are 4 - then also 2 can be removed 2 is left 
    // if there are 5 then 2 can be removed then 3 are left 
    // keep reducing 2 till at max 2 is left
    // (n - 2k) = 1  
    // n - 1/2 = 2k

    mp := make(map[rune]int)

    for _, k := range s {
        if _, ok := mp[k]; ok {
            mp[k] += 1
        } else {
            mp[k] = 1
        }
    }
    reduced := 0
    for _,v := range mp {
        if v >= 3 {
            if (v-1) % 2 == 0 {
                reduced += (v - 1)
            } else {
                reduced += (v - 2)
            }
        }
    }
    return len(s) - reduced
    
}



This post is licensed under CC BY 4.0 by the author.