Advertisement
3223. Minimum Length of String After Operations
UnknownView on LeetCode
Time: O(n)
Space: O(26)
Approach
For each char if frequency ≥ 3 we can reduce; count final length from frequency parities.
3223.cs
C#
// Approach: For each char if frequency ≥ 3 we can reduce; count final length from frequency parities.
// Time: O(n) Space: O(26)
public class Solution
{
public int MinimumLength(string s)
{
int ans = 0;
int[] count = new int[26];
foreach (char c in s)
{
count[c - 'a']++;
}
for (int i = 0; i < 26; i++)
{
if (count[i] > 0)
ans += count[i] % 2 == 0 ? 2 : 1;
}
return ans;
}
}Advertisement
Was this solution helpful?