Approach 1: Two Pointer

class Solution:
    def appendCharacters(self, s: str, t: str) -> int:
        tpos = 0
        for schar in s:
            if schar == t[tpos]:
                tpos += 1
            
            if tpos == len(t):
                break
        
        return len(t) - tpos

Complexity

Time:
Space:

Other Languages

Swift

class Solution {
    func appendCharacters(_ s: String, _ t: String) -> Int {
        var tpos = t.startIndex
        for schar in s {
            if schar == t[tpos] {
                tpos = t.index(after: tpos)
            }
 
            if tpos == t.endIndex {
                break
            }
        }
 
        return t.count - t.distance(from: t.startIndex, to: tpos)
    }
}