15. String Stack
β GFG solution to the String Stack problem: determine if target string can be constructed from pattern using append/delete operations with optimal two-pointer approach. π
π§© Problem Description
π Examples
Example 1
Input: pat = "geuaek", tar = "geek"
Output: true
Explanation: Append the first three characters of pat to s, resulting in s = "geu".
Delete the last character for 'a', leaving s = "ge". Then, append the last two
characters 'e' and 'k' from pat to s, resulting in s = "geek", which matches tar.Example 2
Input: pat = "agiffghd", tar = "gfg"
Output: true
Explanation: Skip the first character 'a' in pat. Append 'g' and 'i' to s,
resulting in s = "gi". Delete the last character for 'f', leaving s = "g".
Append 'f', 'g', and 'h' to s, resulting in s = "gfgh". Finally, delete the
last character for 'd', leaving s = "gfg", which matches tar.Example 3
π Constraints
β
My Approach
Reverse Two-Pointer + Greedy
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated