3412. Permutation Difference between Two Strings
My accepted C++ solution to LeetCode problem 3412, Permutation Difference between Two Strings, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 9.1MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 9.1MB, accepted 2025-12-27.
class Solution {
public:
int findPermutationDifference(string s, string t) {
int pos[26];
for (int i = 0; i < t.size(); i++) pos[t[i] - 'a'] = i;
int sum = 0;
for (int i = 0; i < s.size(); i++) sum += abs(i - pos[s[i] - 'a']);
return sum;
}
};