📜  检查两个堆栈是否相等而不改变

📅  最后修改于: 2021-09-07 05:11:29             🧑  作者: Mango

给定两个堆栈S1S2 ,任务是在不丢失原始堆栈的情况下检查两个堆栈是否以相同的顺序相等。如果两个堆栈相等,则打印“是” 。否则,打印“否”

例子:

方法:可以通过在给定的两个堆栈之间移动一定数量的元素以检查两个堆栈中的每个对应元素来解决给定的问题。请按照以下步骤解决问题:

  • 将堆栈S1S2的大小分别存储在变量NM 中
  • 如果N不等于M ,则打印“ No ”并返回。
  • 迭代范围[1, N]并执行以下操作:
    • 将堆栈S1的顶部(N – i) 个元素推送到堆栈S2
    • 现在,将S1堆栈的顶部元素存储在一个变量中,比如val
    • 现在,将顶部2 * (N – i) 个元素从堆栈S2推送到堆栈S1
    • 如果val的值不等于堆栈S2的顶部值,则打印“”并返回。
    • 否则,通过将顶部(N – i) 个元素从堆栈S1推入堆栈S2来恢复堆栈。
  • 完成上述步骤后,如果以上情况都不满足,则打印“”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to push the elements from
// one stack element into another stack
void pushElements(stack s1, stack s2, int len)
{
    int i = 1;
    while (i <= len) {
 
        // Update the stack
        if (s1.size() > 0) {
            s2.push(s1.top());
            s1.pop();
        }
 
        // Increment i
        i++;
    }
}
 
// Function to compare two given stacks
string compareStacks(stack s1, stack s2)
{
    // Stores the size of S1 stack
    int N = s1.size();
 
    // Stores the size of S2 stack
    int M = s2.size();
 
    // If N is not equal to M
    if (N != M) {
        return "No";
    }
 
    // Traverse the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // Push N-i elements to stack
        // S2 from stack S1
        pushElements(s1, s2, N - i);
 
        // Stores the top value of S1
        int val = s1.top();
 
        // Pushes the 2 * (N-i)
        // elements from S2 to S1
        pushElements(s2, s1, 2 * (N - i));
 
        // If val is not equal
        // to the top of S2
        if (val != s2.top())
            return "No";
 
        // Restores the stacks
        pushElements(s1, s2, N - i);
    }
 
    // Return
    return "Yes";
}
 
// Driver Code
int main()
{
    stack S1, S2;
 
    S1.push(1);
    S1.push(2);
    S1.push(4);
    S1.push(3);
 
    S2.push(1);
    S2.push(2);
    S2.push(4);
    S2.push(3);
 
    cout << (compareStacks(S1, S2));
}
 
// This code is contributed by ukassp.


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to compare two given stacks
    static String compareStacks(
        Stack s1,
        Stack s2)
    {
        // Stores the size of S1 stack
        int N = s1.size();
 
        // Stores the size of S2 stack
        int M = s2.size();
 
        // If N is not equal to M
        if (N != M) {
            return "No";
        }
 
        // Traverse the range [1, N]
        for (int i = 1; i <= N; i++) {
 
            // Push N-i elements to stack
            // S2 from stack S1
            pushElements(s1, s2, N - i);
 
            // Stores the top value of S1
            int val = s1.peek();
 
            // Pushes the 2 * (N-i)
            // elements from S2 to S1
            pushElements(s2, s1, 2 * (N - i));
 
            // If val is not equal
            // to the top of S2
            if (val != s2.peek())
                return "No";
 
            // Restores the stacks
            pushElements(s1, s2, N - i);
        }
 
        // Return
        return "Yes";
    }
 
    // Function to push the elements from
    // one stack element into another stack
    static void pushElements(
        Stack s1, Stack s2,
        int len)
    {
        int i = 1;
        while (i <= len) {
 
            // Update the stack
            s2.push(s1.pop());
 
            // Increment i
            i++;
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        Stack S1 = new Stack<>();
        Stack S2 = new Stack<>();
 
        S1.push(1);
        S1.push(2);
        S1.push(4);
        S1.push(3);
 
        S2.push(1);
        S2.push(2);
        S2.push(4);
        S2.push(3);
 
        System.out.println(
            compareStacks(S1, S2));
    }
}


Python3
# Python3 program for the above approach
 
# Function to compare two given stacks
def compareStacks(s1, s2):
    # Stores the size of S1 stack
    N = len(s1)
 
    # Stores the size of S2 stack
    M = len(s2)
 
    # If N is not equal to M
    if (N != M):
        return "No"
 
    # Traverse the range [1, N]
    for i in range(1, N + 1):
       
        # Push N-i elements to stack
        # S2 from stack S1
        pushElements(s1, s2, N - i)
 
        # Stores the top value of S1
        val = s1[-1]
 
        # Pushes the 2 * (N-i)
        # elements from S2 to S1
        pushElements(s2, s1, 2 * (N - i))
 
        # If val is not equal
        # to the top of S2
        if (val != s2[-1]):
            return "No"
 
        # Restores the stacks
        pushElements(s1, s2, N - i)
 
    # Return
    return "Yes"
 
# Function to push the elements from
# one stack element into another stack
def pushElements(s1, s2, len):
    i = 1
    while (i <= len):
 
        # Update the stack
        s2.append(s1[-1])
        del s1[-1]
 
        # Increment i
        i += 1
 
# Driver Code
if __name__ == '__main__':
    S1 = []
    S2 = []
 
    S1.append(1)
    S1.append(2)
    S1.append(4)
    S1.append(3)
 
    S2.append(1)
    S2.append(2)
    S2.append(4)
    S2.append(3)
 
    print(compareStacks(S1, S2))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
public class GFG {
 
    // Function to compare two given stacks
    static String compareStacks(
        Stack s1,
        Stack s2)
    {
        // Stores the size of S1 stack
        int N = s1.Count;
 
        // Stores the size of S2 stack
        int M = s2.Count;
 
        // If N is not equal to M
        if (N != M) {
            return "No";
        }
 
        // Traverse the range [1, N]
        for (int i = 1; i <= N; i++) {
 
            // Push N-i elements to stack
            // S2 from stack S1
            pushElements(s1, s2, N - i);
 
            // Stores the top value of S1
            int val = s1.Peek();
 
            // Pushes the 2 * (N-i)
            // elements from S2 to S1
            pushElements(s2, s1, 2 * (N - i));
 
            // If val is not equal
            // to the top of S2
            if (val != s2.Peek())
                return "No";
 
            // Restores the stacks
            pushElements(s1, s2, N - i);
        }
 
        // Return
        return "Yes";
    }
 
    // Function to push the elements from
    // one stack element into another stack
    static void pushElements(
        Stack s1, Stack s2,
        int len)
    {
        int i = 1;
        while (i <= len) {
 
            // Update the stack
            s2.Push(s1.Pop());
 
            // Increment i
            i++;
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        Stack S1 = new Stack();
        Stack S2 = new Stack();
 
        S1.Push(1);
        S1.Push(2);
        S1.Push(4);
        S1.Push(3);
 
        S2.Push(1);
        S2.Push(2);
        S2.Push(4);
        S2.Push(3);
 
        Console.WriteLine(
            compareStacks(S1, S2));
    }
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
Yes

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

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