Skip to content

Commit 1db2122

Browse files
authored
Create 1540-can-convert-string-in-k-moves.js
1 parent 77f869f commit 1db2122

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

1540-can-convert-string-in-k-moves.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @param {number} k
5+
* @return {boolean}
6+
*/
7+
const canConvertString = function(s, t, k) {
8+
if(s == null || t == null) return false
9+
const slen = s.length, tlen = t.length
10+
if(slen !== tlen) return false
11+
const count = Array(26).fill(0)
12+
for(let i = 0; i < slen; i++) {
13+
const scode = s.charCodeAt(i)
14+
const tcode = t.charCodeAt(i)
15+
const diff = (tcode - scode + 26) % 26;
16+
if (diff > 0 && diff + count[diff] * 26 > k) {
17+
return false;
18+
}
19+
count[diff]++;
20+
}
21+
return true
22+
};

0 commit comments

Comments
 (0)