File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed
maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,17 @@ public class Lesson13 {
77 * https://leetcode.com/problems/permutation-difference-between-two-strings
88 */
99 public int findPermutationDifference (String s , String t ) {
10- return 0 ;
10+ int ans = 0 ;
11+ int [] indices = new int [26 ];
12+
13+ for (int i = 0 ; i < s .length (); ++i ){
14+ indices [s .charAt (i ) - 'a' ] = i ;
15+ }
16+
17+ for (int i = 0 ; i < t .length (); ++i ){
18+ ans += Math .abs (indices [t .charAt (i ) - 'a' ] - i );
19+ }
20+
21+ return ans ;
1122 }
1223}
Original file line number Diff line number Diff line change 33 * https://leetcode.com/problems/permutation-difference-between-two-strings
44 */
55export function findPermutationDifference ( s : string , t : string ) : number {
6- return 0 ;
6+ let ans : number = 0 ;
7+ let indices : number [ ] = new Array ( 26 ) . fill ( 0 ) ;
8+
9+ for ( let i = 0 ; i < s . length ; ++ i ) {
10+ indices [ s . charCodeAt ( i ) - 'a' . charCodeAt ( 0 ) ] = i ;
11+ }
12+
13+ for ( let i = 0 ; i < t . length ; ++ i ) {
14+ ans += Math . abs ( indices [ t . charCodeAt ( i ) - 'a' . charCodeAt ( 0 ) ] - i ) ;
15+ }
16+ return ans ;
717}
You can’t perform that action at this time.
0 commit comments