Medium
You are given a string s.
You can perform the following process on s any number of times:
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].i that is equal to s[i].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:
s = "bacbcbb".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 <= s.length <= 2 * 105s consists only of lowercase English letters.class Solution {
fun minimumLength(s: String): Int {
val freq = IntArray(26)
for (i in 0..25) {
freq[i] = 0
}
for (i in 0 until s.length) {
freq[s[i].code - 'a'.code]++
}
var c = 0
for (i in freq) {
if (i != 0) {
c += if (i % 2 == 0) {
2
} else {
1
}
}
}
return c
}
}