📌  相关文章
📜  检查所有数组元素是否存在于给定的堆栈中

📅  最后修改于: 2021-05-17 02:17:08             🧑  作者: Mango

给定一个整数S的堆栈和一个整数arr []的数组,任务是检查堆栈中是否存在所有数组元素。

例子:

方法:想法是保持Hashmap中数组元素的频率。现在,当堆栈不为空时,请继续从堆栈中弹出元素,并降低Hashmap中元素的频率。最后,当堆栈为空时,检查哈希图中每个元素的频率是否为零。如果发现是真的,则打印“是”。否则,打印编号。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include
using namespace std;
 
// Function to check if all array
// elements is present in the stack
bool checkArrInStack(stacks, int arr[],
                                  int n)
{
    mapfreq;
     
    // Store the frequency
    // of array elements
    for(int i = 0; i < n; i++)
        freq[arr[i]]++;
         
    // Loop while the elements in the
    // stack is not empty
    while (!s.empty())
    {
        int poppedEle = s.top();
        s.pop();
         
        // Condition to check if the
        // element is present in the stack
        if (freq[poppedEle])
            freq[poppedEle] -= 1;
    }
    if (freq.size() == 0)
        return 0;
         
    return 1;
}
 
// Driver code
int main()
{
    stacks;
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);
    s.push(50);
     
    int arr[] = {20, 30};
     
    int n = sizeof arr / sizeof arr[0];
     
    if (checkArrInStack(s, arr, n))
        cout << "YES\n";
    else
        cout << "NO\n";
}
 
// This code is contributed by Stream_Cipher


Java
// Java program of
// the above approach
import java.util.*;
class GFG{
 
// Function to check if all array
// elements is present in the stack
static boolean checkArrInStack(Stacks,
                               int arr[], int n)
{
  HashMapfreq = new HashMap();
   
  // Store the frequency
  // of array elements
  for(int i = 0; i < n; i++)
    if(freq.containsKey(arr[i]))
      freq.put(arr[i], freq.get(arr[i]) + 1);
  else
    freq.put(arr[i], 1);
 
  // Loop while the elements in the
  // stack is not empty
  while (!s.isEmpty())
  {
    int poppedEle = s.peek();
    s.pop();
 
    // Condition to check if the
    // element is present in the stack
    if (freq.containsKey(poppedEle))
      freq.put(poppedEle, freq.get(poppedEle) - 1);
  }
  if (freq.size() == 0)
    return false;
  return true;
}
 
// Driver code
public static void main(String[] args)
{
  Stack s = new Stack();
  s.add(10);
  s.add(20);
  s.add(30);
  s.add(40);
  s.add(50);
 
  int arr[] = {20, 30};
  int n = arr.length;
 
  if (checkArrInStack(s, arr, n))
    System.out.print("YES\n");
  else
    System.out.print("NO\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program of
# the above approach
 
# Function to check if all array
# elements is present in the stack
def checkArrInStack(s, arr):
  freq = {}
   
  # Store the frequency
  # of array elements
  for ele in arr:
    freq[ele] = freq.get(ele, 0) + 1
   
  # Loop while the elements in the
  # stack is not empty
  while s:
    poppedEle = s.pop()
     
    # Condition to check if the
    # element is present in the stack
    if poppedEle in freq:
      freq[poppedEle] -= 1
       
      if not freq[poppedEle]:
        del freq[poppedEle]
  if not freq:
    return True
  return False
 
# Driver Code
if __name__ == "__main__":
  s = [10, 20, 30, 40, 50]
  arr = [20, 30]
   
  if checkArrInStack(s, arr):
    print("YES")
  else:
    print("NO")


C#
// C# program of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to check if all array
// elements is present in the stack
static bool checkArrInStack(Stacks,
                            int []arr, int n)
{
  Dictionaryfreq = new Dictionary();
   
  // Store the frequency
  // of array elements
  for(int i = 0; i < n; i++)
    if(freq.ContainsKey(arr[i]))
      freq[arr[i]] = freq[arr[i]] + 1;
  else
    freq.Add(arr[i], 1);
 
  // Loop while the elements in the
  // stack is not empty
  while (s.Count != 0)
  {
    int poppedEle = s.Peek();
    s.Pop();
 
    // Condition to check if the
    // element is present in the stack
    if (freq.ContainsKey(poppedEle))
      freq[poppedEle] = freq[poppedEle] - 1;
  }
  if (freq.Count == 0)
    return false;
  return true;
}
 
// Driver code
public static void Main(String[] args)
{
  Stack s = new Stack();
  s.Push(10);
  s.Push(20);
  s.Push(30);
  s.Push(40);
  s.Push(50);
  int []arr = {20, 30};
  int n = arr.Length;
 
  if (checkArrInStack(s, arr, n))
    Console.Write("YES\n");
  else
    Console.Write("NO\n");
}
}
 
// This code is contributed by 29AjayKumar


输出:
YES





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