Shortest way to form a string out of a subsequence of a string The task is taken from LeetCode (subscription required) -

From any string, we can form a subsequence of that string by deleting
some number of characters (possibly no deletions).
Given two strings source and target, return the minimum number of
subsequences of source such that their concatenation equals target. If
the task is impossible, return -1.
Example 1:
Input: source = &quot;abc&quot;, target = &quot;abcbc&quot;
Output: 2
// Explanation: The target &quot;abcbc&quot; can be formed by &quot;abc&quot; and &quot;bc&quot;, which are subsequences of source &quot;abc&quot;.

Example 2:
Input: source = &quot;abc&quot;, target = &quot;acdbc&quot;
Output: -1
// Explanation: The target string cannot be constructed from the subsequences of source string due to the character &quot;d&quot; in target
// string.

Example 3:
Input: source = &quot;xyz&quot;, target = &quot;xzyxz&quot;
Output: 3
// Explanation: The target string can be constructed as follows &quot;xz&quot; + &quot;y&quot; + &quot;xz&quot;.

Note:
Both the source and target strings consist of only lowercase English
letters from &quot;a&quot;-&quot;z&quot;. The lengths of source and target string are
between 1 and 1000.

My solution has space complexity of \$O(n)\$ and time complexity of \$O(n^2)\$ (I think...)
/**
 * @param {string} source
 * @param {string} target
 * @return {number}
 */
var shortestWay = function(source, target) {
    const map = new Map();
    for (let i = 0; i &lt; source.length; i++) {
        if (map.get(source[i])) {
            const arr = map.get(source[i]);
            arr.push(i);
            map.set(source[i], arr);            
        } else {
            map.set(source[i], [i]);
        }
    }
    let occurrences = 0;
    for (let i = 0; i &lt; target.length; i++) {
        const indexes = map.get(target[i]);
        if (indexes === void 0) return -1;
        occurrences++;
        let max = 0;
        indexes.forEach(index =&gt; {
            let j = 0;
            let ignore = 0;
            while(source[index + j + ignore] !== void 0) {
                if(target[i + j] !== source[index + j + ignore]) {
                    ignore++;
                } else {
                    max = Math.max(max, j++);
                }
            }
        });
        i += max;
    }
    return occurrences;
};
I believe your code is O(n^2*m), where n is the target and m is the source. Overall, though, I think there are a lot of readability improvements that could be made here by separating functions and renaming variables (j is a particularly bad contender).
// indexCharacters('hello')
// = {'h':[0],'e':[1],'l':[2,3],'o':[4]}
const indexCharacters = source =&gt; {
    const map = new Map();
    for (let i = 0; i &lt; source.length; i++) {
        const char = source[i];
        if (!map.get(char)) map.set(char,[]);
        map.get(char).push(i);
    }
    return map;
}

// matchLength('abc','abeee') = 2
const matchLength = (source,target) =&gt; {
    let count = 0;
    for (let i=0;i&lt;source.length;++i)
        if (target[count] === source[i])
            ++count;
    return count;
}

const shortestWay = (source, target) =&gt; {
    const all_character_indices = indexCharacters(source);
    let occurrences = 0;
    let targetIndex = 0;
    while (targetIndex &lt; target.length) {
        const starting_indices = all_character_indices.get(target[targetIndex]);
        if (!starting_indices) return -1;
    
        const match_lengths = starting_indices.map(
            sourceIndex=&gt;matchLength(
                source.substr(sourceIndex),
                target.substr(targetIndex)
            )
        );
    
        targetIndex += Math.max(...match_lengths);
        ++occurrences;
    }
    return occurrences;
};

console.log(shortestWay('abc','abcbc'))
console.log(shortestWay('abc','acdbc'))
console.log(shortestWay('xyz','xzyxz'))