---
# System prepended metadata

title: 2506. Count Pairs Of Similar Strings
tags: [Leetcode, Easy, HashMap]

---

# 2506. Count Pairs Of Similar Strings
###### tags: `Leetcode` `Easy` `HashMap`
Link: https://leetcode.com/problems/count-pairs-of-similar-strings/description/
## 思路
把每个word有的所有character转换成mask 存在hashmap里
## Code
```java=
class Solution {
    public int similarPairs(String[] words) {
        Map<Integer, Integer> map = new HashMap<>();
        int ans = 0;
        for(String word:words){
            int mask = getMask(word);
            ans += map.getOrDefault(mask, 0);
            map.put(mask, map.getOrDefault(mask, 0)+1);
        }
        return ans;
    }
    public int getMask(String word){
        int mask = 0;
        for(int i=0; i<word.length(); i++){
            if(((mask>>(word.charAt(i)-'a'))&1)!=1){
                mask += 1<<word.charAt(i)-'a';
            }
        }
        return mask;
    }
}
```