Last active
January 13, 2025 20:58
-
-
Save lbvf50mobile/6b2f002f25e5edf6816de269c3fcf810 to your computer and use it in GitHub Desktop.
Leetcode: 3223. Minimum Lenght of String Arter Operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Leetcode: 3223. Minimum Lenght of String Arter Operations | |
// https://leetcode.com/problems/minimum-length-of-string-after-operations/description/?envType=daily-question&envId=2025-01-13 | |
// = = = = = = = = = = = = = = | |
// Accepted. | |
// Thanks God, Jesus Christ! | |
// = = = = = = = = = = = = = = | |
// Runtime: 11 ms Beats 77.78% | |
// Memory: 8.70 MB Beats 88.89% | |
// 2025.01.13 Daily Challenge. | |
package main | |
import ( | |
// "fmt" | |
) | |
func minimumLength(s string) int { | |
// Substitute each group size of 3 of a paritcular symbol by 1 symbol | |
// untill it possible. | |
var f [26]int | |
ans := len(s) | |
// Create Freq Array. | |
for _, v := range s { | |
i := int(v) - int('a') | |
f[i] += 1 | |
} | |
for _, v := range f { | |
tmp := v | |
for tmp >= 3 { | |
tmp = tmp - (tmp/3)*3 + (tmp / 3) | |
} | |
ans = ans - v + tmp | |
} | |
return ans | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Leetcode: 3223. Minimum Lenght of String Arter Operations | |
// https://leetcode.com/problems/minimum-length-of-string-after-operations/description/?envType=daily-question&envId=2025-01-13 | |
// = = = = = = = = = = = = = = | |
// Accepted. | |
// Thanks God, Jesus Christ! | |
// = = = = = = = = = = = = = = | |
// Runtime: 8 ms Beats 77.78% | |
// Memory: 9.00 MB Beats 44.44% | |
// 2025.01.13 Daily Challenge. | |
// https://gist.github.com/lbvf50mobile/6b2f002f25e5edf6816de269c3fcf810 | |
package main | |
import ( | |
// "fmt" | |
) | |
func minimumLength(s string) int { | |
// Odd => 1 <= because of center. | |
// Even => odd + 1 | |
var f [26]int | |
ans := 0 | |
for _, v := range s { | |
f[v-'a'] += 1 | |
} | |
for _, v := range f { | |
if 0 == v { | |
continue | |
} | |
if 1 == v%2 { | |
ans += 1 | |
} else { | |
ans += 2 | |
} | |
} | |
return ans | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment