πŸ“ Problem Details

πŸ’­What Were My Initial Thoughts?

- need at least 3 elements relating to a particular value in order to conduct a removal operation
- could create a hashmap, where the key is the char and the value is a list of indices where the chars are located 

πŸ€”What Did I Struggle With?

~

πŸ’‘ Explanation of Solution

Observation:
- if a character appears odd times --> 1 character remains
- if a character appears even times --> 2 characcters remain
- instead of tracking indices explicitly , we can just count occurences of each character 
- based on the above observation, we only need to process each character once 

----------------------------------------------------------------------------------

- **Create a frequency hashmap** to count occurrences of each character.
- **For each character**, determine how many will remain after applying the removal rule:
    - If `freq % 2 == 1`, keep `1` character.
    - If `freq % 2 == 0`, keep `2` characters.
- **Sum up the remaining counts** to get the final string length.

βŒ› Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

πŸ’» Implementation of Solution

class Solution {
public:
Β  Β  int minimumLength(string s) {
Β  Β  Β  Β  unordered_map<char,int> map;
Β  Β  Β  Β  int res = 0;
 
Β  Β  Β  Β  for(char c : s) {
Β  Β  Β  Β  Β  Β  map[c]++;
Β  Β  Β  Β  }
 
Β  Β  Β  Β  for(auto it = map.begin(); it != map.end(); it++) {
Β  Β  Β  Β  Β  Β  if(it->second % 2 == 1) res++;
Β  Β  Β  Β  Β  Β  else res += 2;
Β  Β  Β  Β  }
Β  Β  Β  Β  return res;
Β  Β  }
};