Microsoft | OA 2020 | Min deletions to obtain a word with even character occurrences
Anonymous User
3747

You are given a string s. You should return the minimum number of deletions to obtain a word with even frequency of each character.

Constraints:
Input within the a-z charset.

Test cases:

  1. f("acbcbba") => 1, delete 1 'b'
  2. f("axxaxa") => 2, delete 1 'a', 1 'x'
  3. f("kkkkkk") => 0, nop

My solution:

// Time: O(N)
// Space: O(1)
var counter = new int[26];
for (int i = 0; i < S.Length; i++)
    counter[S[i] - 'a']++;

var numRemove = 0;
for (int i = 0; i < counter.Length; i++)
{
    if (counter[i] % 2 == 1)
        numRemove++;
}

return numRemove;
Comments (4)