Skip to content

Commit

Permalink
Aug24: permutaion diffs [E]
Browse files Browse the repository at this point in the history
basic ds: map, vector, time of O(n), easy daily problem
  • Loading branch information
aucker committed Aug 24, 2024
1 parent 9cfdae3 commit 2fa5203
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions daily/Aug24.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

class Solution {
public:
/**
* @brief LC3146: Permutaion diffs between two strings
* TIme: O(N), Space: O(N), use of another ds
*
* @param s
* @param t
* @return int
*/
int findPermutationDifference(string s, string t) {
int ans = 0;
// unordered_map<char, int> smap;
// unordered_map<char, int> tmap;
// for (int i = 0; i < s.length(); i++) {
// smap[s[i]] = i;
// tmap[t[i]] = i;
// }
// for (char c : s) {
// ans += abs(smap[c] - tmap[c]);
// }
// return ans;

int len = s.length();
vector<int> scon(len);
vector<int> tcon(len);
for (int i = 0; i < len; i++) {
scon[i] = s[i];
tcon[i] = t[i];
}

for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (scon[i] == tcon[j]) {
ans += abs(i - j);
}
}
}
return ans;
}
};

0 comments on commit 2fa5203

Please sign in to comment.