Skip to content

Commit cd3518f

Browse files
authored
feat: add solutions to lc problem: No.3146 (doocs#2797)
No.3146.Permutation Difference between Two Strings
1 parent 4c60d04 commit cd3518f

File tree

7 files changed

+169
-6
lines changed

7 files changed

+169
-6
lines changed

solution/3100-3199/3146.Permutation Difference between Two Strings/README.md

+57-3
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,73 @@
6464
<!-- tabs:start -->
6565

6666
```python
67-
67+
class Solution:
68+
def findPermutationDifference(self, s: str, t: str) -> int:
69+
d = {c: i for i, c in enumerate(s)}
70+
return sum(abs(d[c] - i) for i, c in enumerate(t))
6871
```
6972

7073
```java
71-
74+
class Solution {
75+
public int findPermutationDifference(String s, String t) {
76+
int[] d = new int[26];
77+
int n = s.length();
78+
for (int i = 0; i < n; ++i) {
79+
d[s.charAt(i) - 'a'] = i;
80+
}
81+
int ans = 0;
82+
for (int i = 0; i < n; ++i) {
83+
ans += Math.abs(d[t.charAt(i) - 'a'] - i);
84+
}
85+
return ans;
86+
}
87+
}
7288
```
7389

7490
```cpp
75-
91+
class Solution {
92+
public:
93+
int findPermutationDifference(string s, string t) {
94+
int d[26]{};
95+
int n = s.size();
96+
for (int i = 0; i < n; ++i) {
97+
d[s[i] - 'a'] = i;
98+
}
99+
int ans = 0;
100+
for (int i = 0; i < n; ++i) {
101+
ans += abs(d[t[i] - 'a'] - i);
102+
}
103+
return ans;
104+
}
105+
};
76106
```
77107
78108
```go
109+
func findPermutationDifference(s string, t string) (ans int) {
110+
d := [26]int{}
111+
for i, c := range s {
112+
d[c-'a'] = i
113+
}
114+
for i, c := range t {
115+
ans += max(d[c-'a']-i, i-d[c-'a'])
116+
}
117+
return
118+
}
119+
```
79120

121+
```ts
122+
function findPermutationDifference(s: string, t: string): number {
123+
const d: number[] = Array(26).fill(0);
124+
const n = s.length;
125+
for (let i = 0; i < n; ++i) {
126+
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
127+
}
128+
let ans = 0;
129+
for (let i = 0; i < n; ++i) {
130+
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
131+
}
132+
return ans;
133+
}
80134
```
81135

82136
<!-- tabs:end -->

solution/3100-3199/3146.Permutation Difference between Two Strings/README_EN.md

+57-3
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,73 @@
6060
<!-- tabs:start -->
6161

6262
```python
63-
63+
class Solution:
64+
def findPermutationDifference(self, s: str, t: str) -> int:
65+
d = {c: i for i, c in enumerate(s)}
66+
return sum(abs(d[c] - i) for i, c in enumerate(t))
6467
```
6568

6669
```java
67-
70+
class Solution {
71+
public int findPermutationDifference(String s, String t) {
72+
int[] d = new int[26];
73+
int n = s.length();
74+
for (int i = 0; i < n; ++i) {
75+
d[s.charAt(i) - 'a'] = i;
76+
}
77+
int ans = 0;
78+
for (int i = 0; i < n; ++i) {
79+
ans += Math.abs(d[t.charAt(i) - 'a'] - i);
80+
}
81+
return ans;
82+
}
83+
}
6884
```
6985

7086
```cpp
71-
87+
class Solution {
88+
public:
89+
int findPermutationDifference(string s, string t) {
90+
int d[26]{};
91+
int n = s.size();
92+
for (int i = 0; i < n; ++i) {
93+
d[s[i] - 'a'] = i;
94+
}
95+
int ans = 0;
96+
for (int i = 0; i < n; ++i) {
97+
ans += abs(d[t[i] - 'a'] - i);
98+
}
99+
return ans;
100+
}
101+
};
72102
```
73103
74104
```go
105+
func findPermutationDifference(s string, t string) (ans int) {
106+
d := [26]int{}
107+
for i, c := range s {
108+
d[c-'a'] = i
109+
}
110+
for i, c := range t {
111+
ans += max(d[c-'a']-i, i-d[c-'a'])
112+
}
113+
return
114+
}
115+
```
75116

117+
```ts
118+
function findPermutationDifference(s: string, t: string): number {
119+
const d: number[] = Array(26).fill(0);
120+
const n = s.length;
121+
for (let i = 0; i < n; ++i) {
122+
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
123+
}
124+
let ans = 0;
125+
for (let i = 0; i < n; ++i) {
126+
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
127+
}
128+
return ans;
129+
}
76130
```
77131

78132
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int findPermutationDifference(string s, string t) {
4+
int d[26]{};
5+
int n = s.size();
6+
for (int i = 0; i < n; ++i) {
7+
d[s[i] - 'a'] = i;
8+
}
9+
int ans = 0;
10+
for (int i = 0; i < n; ++i) {
11+
ans += abs(d[t[i] - 'a'] - i);
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func findPermutationDifference(s string, t string) (ans int) {
2+
d := [26]int{}
3+
for i, c := range s {
4+
d[c-'a'] = i
5+
}
6+
for i, c := range t {
7+
ans += max(d[c-'a']-i, i-d[c-'a'])
8+
}
9+
return
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int findPermutationDifference(String s, String t) {
3+
int[] d = new int[26];
4+
int n = s.length();
5+
for (int i = 0; i < n; ++i) {
6+
d[s.charAt(i) - 'a'] = i;
7+
}
8+
int ans = 0;
9+
for (int i = 0; i < n; ++i) {
10+
ans += Math.abs(d[t.charAt(i) - 'a'] - i);
11+
}
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def findPermutationDifference(self, s: str, t: str) -> int:
3+
d = {c: i for i, c in enumerate(s)}
4+
return sum(abs(d[c] - i) for i, c in enumerate(t))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function findPermutationDifference(s: string, t: string): number {
2+
const d: number[] = Array(26).fill(0);
3+
const n = s.length;
4+
for (let i = 0; i < n; ++i) {
5+
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
6+
}
7+
let ans = 0;
8+
for (let i = 0; i < n; ++i) {
9+
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)