Approach

Try to let the first char to become ‘a’, and then compare how many strings are the same for each one (with hashmap).

Code

class Solution {
public:
    long long countPairs(vector<string>& words) {
        int m = words.size();
        int n = words[0].size();
        for(int i = 0 ; i < m ; ++i) {
            int shift = words[i][0] - 'a';
            for(int j = 0 ; j < n ; ++j) {
                int curShift = (int(words[i][j] - 'a') - shift + 26 ) % 26;
                words[i][j] = ('a' + curShift);
            }
        }    

        unordered_map<string, int> mp;
        long long totalCnt = 0;
        for(int i = 0 ; i < m ; ++i) {
            if(mp.count(words[i])) totalCnt += mp[words[i]];
            ++mp[words[i]];
        }
        return totalCnt;
    }
};