Skip to content

Commit 39fa4f4

Browse files
authoredAug 10, 2022
Update 1055-shortest-way-to-form-string,js
1 parent 07b7c48 commit 39fa4f4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎1055-shortest-way-to-form-string,js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
/**
2+
* @param {string} source
3+
* @param {string} target
4+
* @return {number}
5+
*/
6+
const shortestWay = function(source, target) {
7+
const a = 'a'.charCodeAt(0), arr = Array(26).fill(0)
8+
for(const ch of source) arr[ch.charCodeAt(0) - a] = 1
9+
let res = 0, j = 0
10+
for(let i = 0, n = source.length; i < n; i++) {
11+
if(arr[target[j].charCodeAt(0) - a] === 0) return -1
12+
if(source[i] === target[j]) j++
13+
if(j === target.length) {
14+
res++
15+
break
16+
}
17+
if(i === n - 1) {
18+
res++
19+
i = -1
20+
}
21+
}
22+
23+
return res
24+
};
25+
26+
// another
27+
128
/**
229
* @param {string} source
330
* @param {string} target

0 commit comments

Comments
 (0)
Please sign in to comment.