Skip to content

Commit b126d79

Browse files
authored
Create 1585-check-if-string-is-transformable-with-substring-sort-operations.js
1 parent 4e092ca commit b126d79

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
const isTransformable = function (s, t) {
7+
const offset = '0'.charCodeAt(0)
8+
const indices = Array.from({ length: 10 }, () => [])
9+
for (let i = s.length - 1; i >= 0; --i) {
10+
indices[s.charCodeAt(i) - offset].push(i)
11+
}
12+
for (const char of t) {
13+
const digit = char.charCodeAt(0) - offset
14+
if (indices[digit].length === 0) return false
15+
const pos = indices[digit][indices[digit].length - 1]
16+
for (let d = 0; d < digit; ++d) {
17+
if (indices[d].length && indices[d][indices[d].length - 1] < pos) {
18+
return false
19+
}
20+
}
21+
indices[digit].pop()
22+
}
23+
return true
24+
}

0 commit comments

Comments
 (0)