diff --git a/solution/3100-3199/3146.Permutation Difference between Two Strings/README.md b/solution/3100-3199/3146.Permutation Difference between Two Strings/README.md index f1eb47840232f..d6716fb2e8c8f 100644 --- a/solution/3100-3199/3146.Permutation Difference between Two Strings/README.md +++ b/solution/3100-3199/3146.Permutation Difference between Two Strings/README.md @@ -64,19 +64,73 @@ ```python - +class Solution: + def findPermutationDifference(self, s: str, t: str) -> int: + d = {c: i for i, c in enumerate(s)} + return sum(abs(d[c] - i) for i, c in enumerate(t)) ``` ```java - +class Solution { + public int findPermutationDifference(String s, String t) { + int[] d = new int[26]; + int n = s.length(); + for (int i = 0; i < n; ++i) { + d[s.charAt(i) - 'a'] = i; + } + int ans = 0; + for (int i = 0; i < n; ++i) { + ans += Math.abs(d[t.charAt(i) - 'a'] - i); + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int findPermutationDifference(string s, string t) { + int d[26]{}; + int n = s.size(); + for (int i = 0; i < n; ++i) { + d[s[i] - 'a'] = i; + } + int ans = 0; + for (int i = 0; i < n; ++i) { + ans += abs(d[t[i] - 'a'] - i); + } + return ans; + } +}; ``` ```go +func findPermutationDifference(s string, t string) (ans int) { + d := [26]int{} + for i, c := range s { + d[c-'a'] = i + } + for i, c := range t { + ans += max(d[c-'a']-i, i-d[c-'a']) + } + return +} +``` +```ts +function findPermutationDifference(s: string, t: string): number { + const d: number[] = Array(26).fill(0); + const n = s.length; + for (let i = 0; i < n; ++i) { + d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i; + } + let ans = 0; + for (let i = 0; i < n; ++i) { + ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i); + } + return ans; +} ``` diff --git a/solution/3100-3199/3146.Permutation Difference between Two Strings/README_EN.md b/solution/3100-3199/3146.Permutation Difference between Two Strings/README_EN.md index a780dc96c6de2..b8cc2875ecd94 100644 --- a/solution/3100-3199/3146.Permutation Difference between Two Strings/README_EN.md +++ b/solution/3100-3199/3146.Permutation Difference between Two Strings/README_EN.md @@ -60,19 +60,73 @@ ```python - +class Solution: + def findPermutationDifference(self, s: str, t: str) -> int: + d = {c: i for i, c in enumerate(s)} + return sum(abs(d[c] - i) for i, c in enumerate(t)) ``` ```java - +class Solution { + public int findPermutationDifference(String s, String t) { + int[] d = new int[26]; + int n = s.length(); + for (int i = 0; i < n; ++i) { + d[s.charAt(i) - 'a'] = i; + } + int ans = 0; + for (int i = 0; i < n; ++i) { + ans += Math.abs(d[t.charAt(i) - 'a'] - i); + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int findPermutationDifference(string s, string t) { + int d[26]{}; + int n = s.size(); + for (int i = 0; i < n; ++i) { + d[s[i] - 'a'] = i; + } + int ans = 0; + for (int i = 0; i < n; ++i) { + ans += abs(d[t[i] - 'a'] - i); + } + return ans; + } +}; ``` ```go +func findPermutationDifference(s string, t string) (ans int) { + d := [26]int{} + for i, c := range s { + d[c-'a'] = i + } + for i, c := range t { + ans += max(d[c-'a']-i, i-d[c-'a']) + } + return +} +``` +```ts +function findPermutationDifference(s: string, t: string): number { + const d: number[] = Array(26).fill(0); + const n = s.length; + for (let i = 0; i < n; ++i) { + d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i; + } + let ans = 0; + for (let i = 0; i < n; ++i) { + ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i); + } + return ans; +} ``` diff --git a/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.cpp b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.cpp new file mode 100644 index 0000000000000..f798990679549 --- /dev/null +++ b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int findPermutationDifference(string s, string t) { + int d[26]{}; + int n = s.size(); + for (int i = 0; i < n; ++i) { + d[s[i] - 'a'] = i; + } + int ans = 0; + for (int i = 0; i < n; ++i) { + ans += abs(d[t[i] - 'a'] - i); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.go b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.go new file mode 100644 index 0000000000000..3508a6ac9d248 --- /dev/null +++ b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.go @@ -0,0 +1,10 @@ +func findPermutationDifference(s string, t string) (ans int) { + d := [26]int{} + for i, c := range s { + d[c-'a'] = i + } + for i, c := range t { + ans += max(d[c-'a']-i, i-d[c-'a']) + } + return +} \ No newline at end of file diff --git a/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.java b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.java new file mode 100644 index 0000000000000..25131d9e38423 --- /dev/null +++ b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public int findPermutationDifference(String s, String t) { + int[] d = new int[26]; + int n = s.length(); + for (int i = 0; i < n; ++i) { + d[s.charAt(i) - 'a'] = i; + } + int ans = 0; + for (int i = 0; i < n; ++i) { + ans += Math.abs(d[t.charAt(i) - 'a'] - i); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.py b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.py new file mode 100644 index 0000000000000..1f1dadcd59fd8 --- /dev/null +++ b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.py @@ -0,0 +1,4 @@ +class Solution: + def findPermutationDifference(self, s: str, t: str) -> int: + d = {c: i for i, c in enumerate(s)} + return sum(abs(d[c] - i) for i, c in enumerate(t)) diff --git a/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.ts b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.ts new file mode 100644 index 0000000000000..b4b513e0fe9d3 --- /dev/null +++ b/solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.ts @@ -0,0 +1,12 @@ +function findPermutationDifference(s: string, t: string): number { + const d: number[] = Array(26).fill(0); + const n = s.length; + for (let i = 0; i < n; ++i) { + d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i; + } + let ans = 0; + for (let i = 0; i < n; ++i) { + ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i); + } + return ans; +}