|
| 1 | +# 2486. Append Characters to String to Make Subsequence |
| 2 | + |
| 3 | +## **Date**: June 3, 2024 |
| 4 | +**Difficulty**:  |
| 5 | +**Related Topics**:   |
| 6 | + |
| 7 | +<p> |
| 8 | + <a href="https://github.com/Hasheditz/Leetcode-CSES-GFG-Codeforces-Coding-Solutions?tab=readme-ov-file#append-characters-to-string-to-make-subsequence" style="margin-right: 5px;"> |
| 9 | + <img src="https://img.shields.io/badge/All%20Problem%20Solutions-green" alt="All Problem Solutions"> |
| 10 | + </a> |
| 11 | + <a href="https://leetcode.com/problems/append-characters-to-string-to-make-subsequence/"> |
| 12 | + <img src="https://img.shields.io/badge/Link%20To%20The%20Question-blue" alt="Link To The Question"> |
| 13 | + </a> |
| 14 | +</p> |
| 15 | + |
| 16 | +## Editorial |
| 17 | + |
| 18 | +This problem requires us to determine the minimum number of characters that need to be appended to the end of a string `s` so that another string `t` becomes a subsequence of `s`. |
| 19 | + |
| 20 | +### Solution Explanation |
| 21 | + |
| 22 | +To solve this problem efficiently, we can use a two-pointer technique. The idea is to iterate through both strings `s` and `t` simultaneously to find the number of characters in `t` that are not in `s`. This will tell us the number of characters we need to append to `s`. |
| 23 | + |
| 24 | +#### Key Steps: |
| 25 | +1. **Initialize Pointers**: Use two pointers, `temp1` for iterating through `s` and `temp2` for iterating through `t`. |
| 26 | +2. **Iterate through the Strings**: |
| 27 | + - If the current character in `t` (`t[temp2]`) matches the current character in `s` (`s[temp1]`), increment both pointers. |
| 28 | + - If they do not match, only increment the pointer for `s` (`temp1`). |
| 29 | +3. **Calculate the Result**: After the loop, the difference between the length of `t` and the pointer `temp2` gives the number of characters that need to be appended. |
| 30 | + |
| 31 | +### Code |
| 32 | + |
| 33 | +```cpp |
| 34 | +class Solution { |
| 35 | +public: |
| 36 | + int appendCharacters(string s, string t) { |
| 37 | + int n = s.size(), m = t.size(); |
| 38 | + int temp1 = 0, temp2 = 0; |
| 39 | + |
| 40 | + while (temp1 < n && temp2 < m) { |
| 41 | + if (t[temp2] == s[temp1]) { |
| 42 | + temp1++; |
| 43 | + temp2++; |
| 44 | + } |
| 45 | + else { |
| 46 | + temp1++; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return m - temp2; |
| 51 | + } |
| 52 | +}; |
| 53 | +``` |
| 54 | +### Complexity Analysis |
| 55 | +- **Time Complexity**: O(n + m), where `n` is the length of `s` and `m` is the length of `t`. This is because we are iterating through both strings once. |
| 56 | +- **Space Complexity**: O(1), as we are using a constant amount of extra space. |
| 57 | + |
| 58 | +## Like and Upvote |
| 59 | + |
| 60 | +If you found this solution helpful, please consider liking 👍 and upvoting ⬆️. Your support helps me to keep providing high-quality solutions. |
| 61 | + |
| 62 | +<p align="center"> |
| 63 | + <img src="https://preview.redd.it/petition-to-change-the-upvote-and-downvote-button-to-like-v0-jbrdq402054c1.jpg?width=640&crop=smart&auto=webp&s=8225d21c98a245f44fd6c1f74a4c6c67f0061f25" width="280"> |
| 64 | +</p> |
0 commit comments