Posts

Showing posts from June, 2024

print first non-repeating character

  /**  * Print1stNonRepeatingCH  */ import java . util .* ; public class Print1stNonRepeatingCH {     public static void Print ( String str ) {         Queue < Character > q = new LinkedList <>();         int freq [] = new int [ 26 ];         for ( int i = 0 ; i < str . length (); i ++ ) {             char ch = str . charAt ( i );             q . add ( ch );             freq [ ch - 'a' ] ++ ;         }         while ( ! q . isEmpty () && freq [ q . peek () - 'a' ] > 1 ) {             q . remove ();         }         if ( q . isEmpty ()) {             System . out . println ( - 1 );         } else { ...

stack using two queues

  import java . util .* ; public class StackWithTwoQueues {     static class Stack {         static Queue < Integer > q1 = new LinkedList <>();         static Queue < Integer > q2 = new LinkedList <>();         public static boolean isEmpty () {             return q1 . isEmpty () && q2 . isEmpty ();         }         public static void push ( int data ) {             if ( ! q1 . isEmpty ()) {                 q1 . add ( data );             } else {                 q2 . add ( data );             }         }         public static int pop () {           ...

queue using two stacks

  import java . util .* ; public class QueueWithTwostacks {     static class Queue {         static Stack < Integer > s1 = new Stack <>();         static Stack < Integer > s2 = new Stack <>();         public static boolean isEmpty () {             return s1 . isEmpty ();         }         // ADD         public static void add ( int data ) {             while ( ! s1 . isEmpty ()) {                 s2 . push ( s1 . pop ());             }             s1 . push ( data );             while ( ! s2 . isEmpty ()) {                 s1 . push ( s2 . pop ());         ...

queue using JCF

  import java . util .* ; public class QueuesWithJCF {     public static void main ( String [] args ) {         Queue < Integer > q = new LinkedList <>();         q . add ( 1 );         q . add ( 2 );         q . add ( 3 );         while ( ! q . isEmpty ()) {             System . out . println ( q . peek ());             q . remove ();         }     } }

circular queue using array

  public class CircularQueues {     static class Queue {         static int arr [];         static int size ;         static int rear ;         static int front ;         Queue ( int n ) {             arr = new int [n];             size = n;             rear = - 1 ;             front = - 1 ;         }         public static boolean isEmpty () {             return rear == - 1 && front == - 1 ;         }         public static boolean isFull () {             return (rear + 1 ) % size == front;         }         // ADD       ...

Queues using LinkedList

  public class QueuesWithLL {     static class Node {         int data ;         Node next ;         Node ( int data ) {             this . data = data ;             this . next = null ;         }     }     static class Queue {         static Node head = null ;         static Node tail = null ;         public static boolean isEmpty () {             return head == null && tail == null ;         }         public static void add ( int data ) {             Node newNode = new Node ( data );             if ( head == null ) {                 he...

Queues

  /**  * Queue  */ public class Queues {     static class Queue {         static int arr [];         static int size ;         static int rear ;         Queue ( int n ) {             arr = new int [ n ];             size = n ;             rear = - 1 ;         }         public static boolean isEmpty () {             return rear == - 1 ;         }         // ADD         public static void add ( int data ) {             if ( rear == size - 1 ) {                 System . out . println ( "Queue is full" );                 return ;...

trapping water using stack

  import java . util .* ; public class TrappingWater {     public static int maxWater ( int arr []) {         int ans = 0 ;         Stack < Integer > s = new Stack <>();         for ( int i = 0 ; i < arr . length ; i ++ ) {             while ( ! s . isEmpty () && arr [ s . peek ()] < arr [ i ]) {                 int pop_height = arr [ s . peek ()];                 s . pop ();                 if ( s . isEmpty ())                     break ;                 int distance = i - s . peek () - 1 ;                 int min_height = Math . min ( arr [ s . peek ()], arr [ i ]) - pop_hei...

decoding strings

  import java . util .* ; public class DecodingAString {     public static String Decode ( String str ) {         Stack < Integer > integerstack = new Stack <>();         Stack < Character > stringstack = new Stack <>();         String temp = "" ;         String result = "" ;         for ( int i = 0 ; i < str . length (); i ++ ) {             int count = 0 ;             if ( Character . isDigit ( str . charAt ( i ))) {                 while ( Character . isDigit ( str . charAt ( i ))) {                     count = count * 10 + str . charAt ( i ) - '0' ;                     i ++ ;          ...

simplifying paths

  import java . util .* ; /**  * SimplifyPath  */ public class SimplifyPath {     public static String Simplify ( String str ) {         Stack < String > s = new Stack < String >();         String res = "" ;         res += "/" ;         int len = str . length ();         for ( int i = 0 ; i < len ; i ++ ) {             String dir = "" ;             while ( i < len && str . charAt ( i ) != '/' ) {                 dir += str . charAt ( i );                 i ++ ;             }             while ( i < len && str . charAt ( i ) == '/' ) {                 i...

palindrome with stacks

import java . util .* ; public class PalindromeWithStacks {     public static class Node {         char data ;         Node next ;         Node ( char data ) {             this . data = data ;             this . next = null ;         }     }     public static Node head ;     public static Node tail ;     public static boolean isPalindrome () {         Stack < Character > s = new Stack <>();         Node temp = head ;         while ( temp != null ) {             s . push ( temp . data );             temp = temp . next ;         }         temp = head ;         while ( temp !=...