DEV Community

Cover image for 2486. Append Characters to String to Make Subsequence
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on • Edited on

2486. Append Characters to String to Make Subsequence

2486. Append Characters to String to Make Subsequence

Medium

You are given two strings s and t consisting of only lowercase English letters.

Return the minimum number of characters that need to be appended to the end of s so that t becomes a subsequence of s.

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

Example 1:

  • Input: s = "coaching", t = "coding"
  • Output: 4
  • Explanation: Append the characters "ding" to the end of s so that s = "coachingding".\ Now, t is a subsequence of s ("coaching*ding*").\ It can be shown that appending any 3 characters to the end of s will never make t a subsequence.

Example 2:

  • Input: s = "abcde", t = "a"
  • Output: 0
  • Explanation: t is already a subsequence of s ("abcde").

Example 3:

  • Input: s = "z", t = "abcde"
  • Output: 5
  • Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".\ Now, t is a subsequence of s ("z*abcde*").\ It can be shown that appending any 4 characters to the end of s will never make t a subsequence.

Constraints:

  • 1 <= s.length, t.length <= 105
  • s and t consist only of lowercase English letters.

Solution:

class Solution {

    /**
     * @param String $s
     * @param String $t
     * @return Integer
     */
    function appendCharacters($s, $t) {
        $i = 0;

        foreach(str_split($s) as $c) {
            if ($c == $t[$i]) {
                if (++$i == strlen($t)) {
                    return 0;
                }
            }
        }

        return strlen($t) - $i;
    }
}
Enter fullscreen mode Exit fullscreen mode

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay