File tree 7 files changed +169
-6
lines changed
solution/3100-3199/3146.Permutation Difference between Two Strings
7 files changed +169
-6
lines changed Original file line number Diff line number Diff line change 64
64
<!-- tabs:start -->
65
65
66
66
``` 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))
68
71
```
69
72
70
73
``` 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
+ }
72
88
```
73
89
74
90
``` 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
+ };
76
106
```
77
107
78
108
```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
+ ```
79
120
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
+ }
80
134
```
81
135
82
136
<!-- tabs: end -->
Original file line number Diff line number Diff line change 60
60
<!-- tabs:start -->
61
61
62
62
``` 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))
64
67
```
65
68
66
69
``` 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
+ }
68
84
```
69
85
70
86
``` 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
+ };
72
102
```
73
103
74
104
```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
+ ```
75
116
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
+ }
76
130
```
77
131
78
132
<!-- tabs: end -->
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments