Posts

Showing posts from November, 2021

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()) {                                  String s=sc.next();                  //aa.add(str);                          for ( int  i= 0 ;i<s.length();i++){                                   //int count=0;                  //String input=aa.get(i1);                  //for(int i=0;i<input.length();i++){                      if (stack.isEmpty()){                         stack.push(s.charAt(i));                     }                      else   if (stack.peek()== '{'  && s.charAt(i)== '}' ){                         stack.pop();                     }  

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);             }             else{                 if(stack.peek()>nums2[i]){                     aa.add(stack.peek());                 }                 else{                     while(!stack.isEmpty() && stack.peek()<=nums2[i]){                         stack.pop();                     }                     if(stack.isEmpty()){                         aa.add(-1);                     }        

Lonely Integer HackerRank

 Problem Link: https://www.hackerrank.com/challenges/one-week-preparation-kit-lonely-integer/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=preparation-kits&playlist_slugs%5B%5D=one-week-preparation-kit&playlist_slugs%5B%5D=one-week-day-two https://www.hackerrank.com/challenges/lonely-integer/problem Solution: Approach 1: Using Collections.frequency() method int a=0;         for(int i=0;i<aa.size();i++){             if(Collections.frequency(aa,aa.get(i))==1){                 a=aa.get(i);                 break;             }         }  return a;          For unique elements, the frequency will be 1 only. So we will traverse the whole array and find the element with one frequency.  Once we find it then we will store it in the integer variable and return it. Time:O(N^2)[Collections.frequency() method is used. For every element, we will see frequency. In the worst case we will traverse the whole array and get the element at last.] Space:O(N)[ArrayList is Us

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. ---------------------------------------------------------------------------------------------------------------------------         public String removeDuplicates(String s) {         Stack<Ch

Relative Sort Array LeetCode

 Two Approaches: Explaination::::::::::::::::::::::::::::::::::::: First, we will add all the elements of arr1 to the hashmap with the key as element and value as their frequency. Second, we will traverse the arr2 as a take-up element one by one. If that element is present in the map (as a key) then we will take out frequency(ie. value) and store that element up to its frequency in the ArrayList(ac). This ArrayList ac contains the relative order of elements that are duplicated (elements present in both arrays.) Now for the remaining elements which are present in arr1 but not in arr2. We will store the whole given array arr1 in an ArrayList aa and using the removeAll method ie by using aa.removeAll(ac) we will get aa without any duplicates. By usage Of Collections. sort(aa) we will get the sorted ArrayList. Now we will append both by using ac.addAll(aa). And after converting ac to an array, we will return it. The same thing we can do by using an additional LinkedHashSet which will store