[LeetCode] Find Maximum Number of String Pairs

2744. Find Maximum Number of String Pairs

You are given a 0-indexed array words consisting of distinct strings.

The string words[i] can be paired with the string words[j] if:

  • The string words[i] is equal to the reversed string of words[j].
  • 0 <= i < j < words.length.

Return the maximum number of pairs that can be formed from the array words.

Note that each string can belong in at most one pair.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int maximumNumberOfStringPairs(vector<string>& words) {
unordered_set<string> us;
int res = 0;
for(auto w : words) {
if(us.count(w)) res += 1;
reverse(begin(w), end(w));
us.insert(w);
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/25/PS/LeetCode/find-maximum-number-of-string-pairs/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.