📌  相关文章
📜  检查一个字符串是否是另一个字符串(使用 Stacks )

📅  最后修改于: 2021-09-06 06:26:04             🧑  作者: Mango

给定一个字符串S,任务是使用堆栈检查字符串目标是否是字符串S的子序列。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化堆栈s
  • 迭代字符串target的字符。

目标压入堆栈

  • 反向遍历字符串S。
  • 如果字符串S 的当前字符与栈顶字符相同,则弹出栈顶元素。

S 中遍历

S 中遍历

从堆栈中弹出

在 S 中遍历

从堆栈中弹出

在 st 中穿越

从堆栈中弹出

在 S 中遍历

堆栈变空

  • 如果在任何时候堆栈变空,则可以断定targetS的子序列。
  • 否则, target不是S的子序列。

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Function to check if target
// is a subsequence of string S
void checkforSubsequence(string S,
                         string target)
{
 
    // Declare a stack
    stack s;
 
    // Push the characters of
    // target into the stack
    for (int i = 0; i < target.size(); i++) {
        s.push(target[i]);
    }
 
    // Traverse the string S in reverse
    for (int i = (int)S.size() - 1; i >= 0; i--) {
 
        // If the stack is empty
        if (s.empty()) {
 
            cout << "Yes" << endl;
            return;
        }
 
        // if S[i] is same as the
        // top of the stack
        if (S[i] == s.top()) {
 
            // Pop the top of stack
            s.pop();
        }
    }
 
    // Stack s is empty
    if (s.empty())
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
 
// Driver Code
int main()
{
    string S = "KOTTAYAM";
    string target = "KOTA";
 
    checkforSubsequence(S, target);
 
    return 0;
}


Java
// Java approach for the above approach
import java.util.Stack;
 
public class GFG {
 
    // Function to check if target
    // is a subsequence of string S
    static void checkforSubsequence(String S, String target)
    {
 
        // Declare a stack
        Stack s = new Stack<>();
 
        // Push the characters of
        // target into the stack
        for (int i = 0; i < target.length(); i++) {
            s.push(target.charAt(i));
        }
 
        // Traverse the string S in reverse
        for (int i = (int)S.length() - 1; i >= 0; i--) {
 
            // If the stack is empty
            if (s.empty()) {
 
                System.out.println("Yes");
                return;
            }
 
            // if S[i] is same as the
            // top of the stack
            if (S.charAt(i) == s.peek()) {
 
                // Pop the top of stack
                s.pop();
            }
        }
 
        // Stack s is empty
        if (s.empty())
            System.out.println("Yes");
        else
            System.out.println("No");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "KOTTAYAM";
        String target = "KOTA";
 
        checkforSubsequence(S, target);
    }
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to check if target
# is a subsequence of string S
def checkforSubsequence(S, target):
 
    # Declare a stack
    s = []
 
    # Push the characters of
    # target into the stack
    for i in range(len(target)):
        s.append(target[i])
 
    # Traverse the string S in reverse
    for i in range(len(S) - 1, -1, -1):
 
        # If the stack is empty
        if (len(s) == 0):
            print("Yes")
            return
 
        # If S[i] is same as the
        # top of the stack
        if (S[i] == s[-1]):
 
            # Pop the top of stack
            s.pop()
 
    # Stack s is empty
    if (len(s) == 0):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == "__main__":
 
    S = "KOTTAYAM"
    target = "KOTA"
 
    checkforSubsequence(S, target)
     
# This code is contributed by ukasp


C#
// C# approach for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check if target
// is a subsequence of string S
static void checkforSubsequence(String S,
                                String target)
{
     
    // Declare a stack
    Stack s = new Stack();
 
    // Push the characters of
    // target into the stack
    for(int i = 0; i < target.Length; i++)
    {
        s.Push(target[i]);
    }
 
    // Traverse the string S in reverse
    for(int i = (int)S.Length - 1; i >= 0; i--)
    {
 
        // If the stack is empty
        if (s.Count == 0)
        {
            Console.WriteLine("Yes");
            return;
        }
 
        // If S[i] is same as the
        // top of the stack
        if (S[i] == s.Peek())
        {
             
            // Pop the top of stack
            s.Pop();
        }
    }
 
    // Stack s is empty
    if (s.Count == 0)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver Code
public static void Main(String[] args)
{
    String S = "KOTTAYAM";
    String target = "KOTA";
 
    checkforSubsequence(S, target);
}
}
 
// This code is contributed by shikhasingrajput


输出:
Yes

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live