Shortest Way to Form String

medium greedy two pointers string

Problem

A subsequence of a string is formed by deleting zero or more characters without changing the order of the rest. Given two strings source and target, return the minimum number of subsequences of source whose concatenation equals target. If it is impossible (target uses a character not in source), return -1.

Inputsource = "abc", target = "abcbc"
Output2
"abc" yields the subsequence "abc", then a second pass yields "bc". Together they form "abcbc".

def shortest_way(source, target):
    if not set(target) <= set(source):
        return -1
    count = 0
    j = 0
    while j < len(target):
        count += 1
        for ch in source:
            if j < len(target) and target[j] == ch:
                j += 1
    return count
function shortestWay(source, target) {
  const have = new Set(source);
  for (const ch of target) if (!have.has(ch)) return -1;
  let count = 0, j = 0;
  while (j < target.length) {
    count++;
    for (const ch of source) {
      if (j < target.length && target[j] === ch) j++;
    }
  }
  return count;
}
class Solution {
    public int shortestWay(String source, String target) {
        boolean[] have = new boolean[26];
        for (char c : source.toCharArray()) have[c - 'a'] = true;
        for (char c : target.toCharArray()) if (!have[c - 'a']) return -1;
        int count = 0, j = 0;
        while (j < target.length()) {
            count++;
            for (int k = 0; k < source.length(); k++) {
                if (j < target.length() && target.charAt(j) == source.charAt(k)) j++;
            }
        }
        return count;
    }
}
int shortestWay(string source, string target) {
    bool have[26] = {false};
    for (char c : source) have[c - 'a'] = true;
    for (char c : target) if (!have[c - 'a']) return -1;
    int count = 0, j = 0;
    while (j < (int)target.size()) {
        count++;
        for (char ch : source) {
            if (j < (int)target.size() && target[j] == ch) j++;
        }
    }
    return count;
}
Time: O(n · m) Space: O(1)