Skip to content
LC-2486 Medium LeetCode

2486. Append Characters to String to Make Subsequence

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 73% Topics: Two Pointers, String, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# two pointers, greedy
class Solution(object):
    def appendCharacters(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: int
        """
        i = -1
        for j, c in enumerate(t):
            for i in xrange(i+1, len(s)):
                if s[i] == c:
                    break
            else:
                return len(t)-j
        return 0

Solution from kamyu104/LeetCode-Solutions · MIT