File tree 2 files changed +47
-1
lines changed
2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,354 LeetCode solutions in JavaScript
1
+ # 1,355 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1176
1176
1536|[ Minimum Swaps to Arrange a Binary Grid] ( ./solutions/1536-minimum-swaps-to-arrange-a-binary-grid.js ) |Medium|
1177
1177
1537|[ Get the Maximum Score] ( ./solutions/1537-get-the-maximum-score.js ) |Hard|
1178
1178
1539|[ Kth Missing Positive Number] ( ./solutions/1539-kth-missing-positive-number.js ) |Easy|
1179
+ 1540|[ Can Convert String in K Moves] ( ./solutions/1540-can-convert-string-in-k-moves.js ) |Medium|
1179
1180
1550|[ Three Consecutive Odds] ( ./solutions/1550-three-consecutive-odds.js ) |Easy|
1180
1181
1551|[ Minimum Operations to Make Array Equal] ( ./solutions/1551-minimum-operations-to-make-array-equal.js ) |Medium|
1181
1182
1566|[ Detect Pattern of Length M Repeated K or More Times] ( ./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1540. Can Convert String in K Moves
3
+ * https://leetcode.com/problems/can-convert-string-in-k-moves/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given two strings s and t, your goal is to convert s into t in k moves or less.
7
+ *
8
+ * During the ith (1 <= i <= k) move you can:
9
+ * - Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen
10
+ * in any previous move, and shift the character at that index i times.
11
+ * - Do nothing.
12
+ *
13
+ * Shifting a character means replacing it by the next letter in the alphabet (wrapping around so
14
+ * that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times.
15
+ *
16
+ * Remember that any index j can be picked at most once.
17
+ *
18
+ * Return true if it's possible to convert s into t in no more than k moves, otherwise return false.
19
+ */
20
+
21
+ /**
22
+ * @param {string } s
23
+ * @param {string } t
24
+ * @param {number } k
25
+ * @return {boolean }
26
+ */
27
+ var canConvertString = function ( source , target , maxMoves ) {
28
+ if ( source . length !== target . length ) return false ;
29
+ const shiftCounts = new Array ( 26 ) . fill ( 0 ) ;
30
+
31
+ for ( let i = 0 ; i < source . length ; i ++ ) {
32
+ const shiftNeeded = ( target . charCodeAt ( i ) - source . charCodeAt ( i ) + 26 ) % 26 ;
33
+ if ( shiftNeeded > 0 ) {
34
+ shiftCounts [ shiftNeeded ] ++ ;
35
+ }
36
+ }
37
+
38
+ for ( let shift = 1 ; shift < 26 ; shift ++ ) {
39
+ if ( shiftCounts [ shift ] === 0 ) continue ;
40
+ const totalMoves = shift + 26 * ( shiftCounts [ shift ] - 1 ) ;
41
+ if ( totalMoves > maxMoves ) return false ;
42
+ }
43
+
44
+ return true ;
45
+ } ;
You can’t perform that action at this time.
0 commit comments