From 2fa5203db0e16db2bb5bd3de03d2eec970fa979c Mon Sep 17 00:00:00 2001 From: aucker Date: Sat, 24 Aug 2024 10:42:28 +0800 Subject: [PATCH] Aug24: permutaion diffs [E] basic ds: map, vector, time of O(n), easy daily problem --- daily/Aug24.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 daily/Aug24.cc diff --git a/daily/Aug24.cc b/daily/Aug24.cc new file mode 100644 index 0000000..9bca774 --- /dev/null +++ b/daily/Aug24.cc @@ -0,0 +1,46 @@ +#include +#include +#include +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 smap; + // unordered_map 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 scon(len); + vector 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; + } +};