Posts

Showing posts from March, 2021

Remove Outermost Parentheses LeetCode Solution - The Coding Shala

Home >> LeetCode >> Remove Outermost Parentheses  In this post, we will learn how to solve LeetCode's Remove Outermost Parentheses problem and will implement its solution in Java. Remove Outermost Parentheses Problem A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation.  For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings. Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S. Example 1: Input: "(()())(())" Output: "()()()" Explanation:  The input string is "(()())(())", with primitive decomposition "(()())" + "(())". After removing outer parentheses of each part, this is "()()" + "()" = "()()()". Example 2: Input: "(()()...

Implement Queue using Linked List - The Coding Shala

Home >> Data Structure >> Implement queue using linked list  In this post, we will learn how to implement Queue using Linked List and will write a Java Program for the same. Implement Queue using Linked List We will implement Queue using Linked List. The basic operation of the queue like push/offer and remove/poll method will implement here. Java Program:  class QueueNode { int data ; QueueNode next ; QueueNode ( int a ) { data = a ; next = null ; } } class MyQueue { QueueNode front , rear ; // This function should add an item at // rear void push ( int a ) { QueueNode newNode = new QueueNode ( a ); if ( front == null || rear == null ) { front = newNode ; rear = front ; } else { rear . next = newNode ; rear = rear . next ; } } // This function should remove front // item from queue and should re...

Implement Queue Using Array - The Coding Shala

Home >> Data Structures >> Implement Queue Using Array  In this post, we will learn how to Implement Queue Using Array and will write a Java program for the same. Implement Queue Using Array We will write a simple Java Program to implement queue using the array, Only Push and Pop method we will implement here. Java Program:  class MyQueue { int front , rear ; int arr [] = new int [ 100005 ]; MyQueue () { front = 0 ; rear = 0 ; } /* The method push to push element into the queue */ void push ( int x ) { arr [ rear ] = x ; rear ++; } /* The method pop which return the element poped out of the queue*/ int pop () { if ( front == rear ) return - 1 ; int res = arr [ front ]; front ++; return res ; } } Other Posts You May Like Queue Data Structure Circular Queue Data Structure Stack Data Structure Implement Stack Using Array Implem...

Number of Recent Calls LeetCode Solution - The Coding Shala

Home >> LeetCode >> Number of Recent Calls  In this post, we will learn how to solve LeetCode's Number of Recent Calls Problem and will implement its solution in Java. Number of Recent Calls Problem You have a RecentCounter class that counts the number of recent requests within a certain time frame. Implement the RecentCounter class: RecentCounter() Initializes the counter with zero recent requests. int ping(int t) Adds a new request at time t, where t represents some time in milliseconds and returns the number of requests that have happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t]. It is guaranteed that every call to ping uses a strictly larger value of t than the previous call. Example 1: Input: ["RecentCounter", "ping", "ping", "ping", "ping"] [[], [1], [100], [3001], [3002]] Output: [null, 1, ...