Rust/Python Linear time/space with explanation
Intuition\nStore the counts of all strings in the hashmap. Now iterate over all our strings and find their reversed strings. Check if it exists in a hashmap. \n
# Intuition\nStore the counts of all strings in the hashmap. Now iterate over all our strings and find their reversed strings. Check if it exists in a hashmap. \n\nNow we have two possible options:\n - reverse string is equal to our original string. To be able to find a reverse we need to have at least two values in a counter `map[rev] >= 2`. If this is the case, increment the results and decrease counter by 2\n - otherwise to be able to find a reverse, you need to have at least one value in a counter. In this case also increment the result and decrease counter by 1.\n\n# Complexity\n- Time complexity: $O(n)$\n- Space complexity: $O(n)$\n\n# Code\n\n```Rust []\nuse std::collections::HashMap;\n\nimpl Solution {\n  pub fn maximum_number_of_string_pairs(arr: Vec<String>) -> i32 {\n    let mut map: HashMap<String, i32> = HashMap::new();\n    for v in &arr {\n      *map.entry(v.to_string()).or_default() += 1;\n    }\n\n    let mut res = 0;\n    for v in arr {\n      let v_rev: String = v.chars().rev().collect();\n      if !map.contains_key(&v_rev) {\n        continue;\n      }\n      if v == v_rev {\n        if *map.get(&v_rev).unwrap() > 1 {\n          res += 1;\n          *map.entry(v_rev.to_string()).or_default() -= 2;\n        }\n      } else {\n        if *map.get(&v_rev).unwrap() > 0 {\n          res += 1;\n          *map.entry(v_rev.to_string()).or_default() -= 1;\n        }\n      }\n    }\n\n    return res / 2;\n  }\n}\n```\n```python []\nfrom collections import Counter\n\nclass Solution:\n  def maximumNumberOfStringPairs(self, words: List[str]) -> int:\n    cnt, res = Counter(words), 0\n    for v in words:\n      v_rev = v[::-1]\n      if v == v_rev:\n        if cnt[v] >= 2:\n          res += 1\n          cnt[v] -= 2\n      else:\n        if cnt[v_rev] >= 1:\n          res += 1;\n          cnt[v_rev] -= 1\n    \n    return res // 2\n```\n
['Python3', 'Rust']