3146.cpp (370B)
1 class Solution { 2 public: 3 int findPermutationDifference(const string &s, const string &t) const { 4 static int pos[26]; 5 int res = 0; 6 7 for (int i = 0; i < size(s); i++) { 8 pos[s[i] - 'a'] = i; 9 } 10 11 for (int i = 0; i < size(t); i++) { 12 res += abs(pos[t[i] - 'a'] - i); 13 } 14 15 return res; 16 } 17 };