Posts

Showing posts with the label Stack

Java Stack HackerRank

  import  java.util.*; class  Solution{           public   static   void  main(String []argh)     {         Scanner sc =  new  Scanner(System.in);          /*String s=sc.next();         /*ArrayList<String>aa=new ArrayList<String>();          ArrayList<String>ab=new ArrayList<String>();*/         Stack<Character>stack= new  Stack<Character>();                       while  (sc.hasNext()) {                      ...

Next Greater Element I LeetCode

 Problem Link: https://leetcode.com/problems/next-greater-element-i/ Solution:           Approach 1 Using 2 arraylist and contains method                          Stack<Integer> stack=new Stack<Integer>();         ArrayList<Integer> aa=new ArrayList<Integer>();         ArrayList<Integer> ab=new ArrayList<Integer>();         for(int i=0;i<nums2.length;i++){             ab.add(nums2[i]);         }                  for(int i=nums2.length-1;i>-1;i--){             if(stack.isEmpty()){                 stack.push(nums2[i]);                 aa.add(-1);           ...

Remove All Adjacent Duplicates In A String LeetCode

  Approach 1: Using Stack : 1. We will create a Stack Of Characters. 2. We will traverse the given string from last. 3.   If the stack is empty we will push the i'th indexed character of string into the stack.     Else, we will see whether the peak of the stack is equal to the i'th character of the string.                    If it is equal it means adjacent duplicates are found and hance we will pop the stack element.                    else we will push that character into the stack.[It means that the character is different from the stack.peek()]     In this way, we will traverse the whole string from last to end. After traversing we will empty the stack and append all the characters into the StringBuilder object and return to the main function after converting that object to a string. ----------------------------------------------------------...

Largest Rectangle In Histogram InterviewBit/ LeetCode

  class  StoreStack{      int  data;      int  val; } public   class  Solution {      public   int  largestRectangleArea( int [] arr) {          int  n=arr.length;         Stack<StoreStack> stack1= new  Stack<StoreStack>();          Stack<StoreStack> stack2= new  Stack<StoreStack>();                   ArrayList<Integer>aa= new  ArrayList<Integer>();         ArrayList<Integer>ab= new  ArrayList<Integer>();         ArrayList<Integer>ac= new  ArrayList<Integer>();      ...

Stock Span Problem Geeks For Geeks

 class StoreStack{ int val; int data; } class Solution {     //Function to calculate the span of stock’s price for all n days.          public static int[] calculateSpan(int arr[], int n)     {         ArrayList<Integer> aa=new ArrayList<Integer>();         StoreStack obj[]=new StoreStack[n];         Stack<StoreStack> stack=new Stack<StoreStack>();         for(int i=0;i<n;i++){ obj[i]=new StoreStack(); obj[i].val=arr[i]; obj[i].data=i; } /*for(int i=0;i<n;i++){ System.out.println(obj[i].val+" "+obj[i].data); }*/ for(int i=0;i<n;i++){ if(stack.isEmpty()){ aa.add(-1); } else{ if(stack.peek().val>arr[i]){ aa.add(stack.peek().data); } else{ while(!stack.isEmpty() && stack.peek().val<=arr[i]){ stack.pop(); } if(stack...

Next Greater Node In Linked List LeetCode

 class Solution {     public int[] nextLargerNodes(ListNode head) {         ArrayList<Integer> aa=new ArrayList<Integer>();         Stack<Integer> stack=new Stack<Integer>();                           ListNode temp=head;                 while(temp!=null){             aa.add(temp.val);             temp=temp.next;         }         Collections.reverse(aa);                  System.out.println(aa);                  int ab[]=new int[aa.size()];                  for(int i=0;i<aa.size();i++){             if(stack.isEmpty()){      ...

Next Greater Element Geeks For Geeks And InterviewBit

 Approach 1: Using ArrayList                  ArrayList<Long> aa=new ArrayList<Long>();         Stack<Long>stack=new Stack<Long>();         for(int i=n-1;i>-1;i--){             if(stack.isEmpty()==true){                 aa.add(Long.valueOf(-1));             }             else{                 if(stack.peek()>arr[i]){                     aa.add(stack.peek());                 }                 else{                     while(!stack.isEmpty() && stack.peek()<=arr[i]){               ...

Longest SubString Without Repeating Characters LeetCode

         Way 1 : Using ArrayDeque and ArrayList           int count=0;         int index=0;         ArrayList<Integer>aa=new ArrayList<Integer>();         ArrayDeque<Character> arr=new ArrayDeque<Character>();         for(int i=0;i<s.length();i++){             if(arr.contains(s.charAt(i))){                 aa.add(count); count=0;                 arr.clear(); i=index++;                              }             else{                 arr.addLast(s.charAt(i)); count++;             }         }       ...

Remove Consecutive Characters Geeks For Geeks

 class Solution{     public String removeConsecutiveCharacter(String s){         Stack<Character> stack=new Stack<Character>(); int count=0; for(int i=0;i<s.length();i++){ if(stack.isEmpty()==true){ stack.push(s.charAt(i)); } else if(String.valueOf(stack.peek()).equals(String.valueOf(s.charAt(i)))){ count++; } else stack.push(s.charAt(i)); } //System.out.println(stack); StringBuffer sb=new StringBuffer(); while(!stack.isEmpty()){     sb.append(stack.pop()); } sb.reverse(); return sb.toString();     } } APPROACH: Here we will use a stack. We will add the elements of string in the stack in such a manner that if an element is equal to the peek of the stack it won't get added to the stack otherwise it will be added in the stack. I have used a counter variable also which will count the number of times different duplicate elements were traversed. After...

Reverse A String Using Stack Geeks For Geeks

 class Solution {          public String reverse(String s){         Stack<Character> stack=new Stack<Character>(); for(int i=0;i<s.length();i++){ stack.push(s.charAt(i)); } StringBuffer sb=new StringBuffer(); while(!stack.isEmpty()){ sb.append(stack.pop()); } String s1=sb.toString(); return s1;     } } Approach: We will add all the elements of the given string into the stack. After that, we will apply a while loop which will run till the stack becomes empty and the object of StringBuffer will append those characters. In the end, we will convert the string buffer object to a string.  TIME: O(N) [here N is the length of given string][GFG time: 0.2/1.5] SPACE: O(1) [here we are simply appending the characters in the last and later converting object of StringBuffer into a string.] Thanks for Reading 😇.

Array Formation HackerEarth

  import java . util .*; import java . util . LinkedList ; import java . util . Queue ;   class PrimeChecker { Queue < Integer > q ; Stack < Integer > stack ; Stack < Integer > stack1 ; public PrimeChecker (){ q = new LinkedList < Integer >(); stack = new Stack < Integer >(); } public void isPrime ( int num ){ boolean b = true ; for ( int i = 2 ; i <= num / 2 ; i ++) { int temp = num % i ; if ( temp == 0 ) { b = false ; break ; } } if ( b ){ //System.out.println(num + " is a Prime Number"); q . add ( num ); } else { //System.out.println(num + " is not a Prime Number"); stack . push ( num ); } } // This is checked so that we can skip // middle five numbers in below loop public void toPrint (){ //System.out.println(/*"Queue is: "+*/ q); //System.out.println(/*...