We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 07b7c48 commit 39fa4f4Copy full SHA for 39fa4f4
1055-shortest-way-to-form-string,js
@@ -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
19
+ i = -1
20
21
22
+
23
+ return res
24
+};
25
26
+// another
27
28
/**
29
* @param {string} source
30
* @param {string} target
0 commit comments