Skip to main content

Posts

Showing posts with the label Stack

Valid Parentheses Question Solution in java

  Valid Parentheses Question Solution in java Code:  import java.util.*; class Test { public static void main(String[] args){ Scanner scn = new Scanner(System.in); String s = scn.next(); System.out.println(isValid(s)); } public static boolean isValid(String s) {        Stack<Character> st = new Stack<>();         for(int i=0;i<s.length();i++){             char ch = s.charAt(i);             if(ch=='('||ch=='['||ch=='{'){                 st.push(ch);             }else if(st.isEmpty()){                 return false;             }else { char top = st.pop();                 if(ch==')'&&top!='('){                     return false;                 } if(ch=='}'&&top!='{'){                     return false;                 } if(ch==']'&&top!='['){                     return false;                 }             }         } return st.isEmpty();     } }

Reverse a Stack using Recursion

 Reverse a Stack using Recursion Code:: import java.util.Stack; public class Test { public static void main(String[] args) { Stack<Integer> st = new Stack<Integer>(); st.add(1); st.add(2); st.add(3); st.add(4); System.out.println(st); reverse(st); System.out.println(st); } public static void reverse(Stack<Integer> s) { if(s.size()==0) { return; } int temp = s.pop(); reverse(s); insert(s,temp); } public static void insert(Stack<Integer> s,int temp) { if(s.size()==0) { s.push(temp); return; } int temp1 = s.pop(); insert(s,temp); s.push(temp1); } }

Maximum Element Hackerrank Solution Java | Hackerrank Data Structure

 Maximum Element Hackerrank Solution Java For Explanation Video: Sample Input STDIN Function ----- -------- 10 operations[] size n = 10 1 97 operations = ['1 97', '2', '1 20', ....] 2 1 20 2 1 26 1 20 2 3 1 91 3 Sample Output 26 91 Code: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; public   class  Solution {      public   static   void  main(String[] args){          Scanner scn = new Scanner(System.in);         int n = scn.nextInt();         Stack<Integer> st = new Stack<Integer>();         PriorityQueue<Integer> heap = new PriorityQueue<>(Collections.reverseOrder());         while(n-->0){             int t = scn.nextInt();             switch(

Reverse a string using Stack in java

 Reverse a string using Stack in java Explanation Of code: Code: import java.util.Stack; class Test{ public static void main(String[] args){ String s = "Baburao"; System.out.println(reverse(s));     } public static String reverse(String s){ Stack<Character> st = new Stack<>(); String reverse = ""; for(int i=0;i<s.length();i++){ st.push(s.charAt(i)); } while(st.size()!=0){ reverse = reverse + st.pop(); } return reverse; } } o/p:oarubaB