diff --git a/stacks/README.md b/stacks/README.md index 34cd53b8..9b853572 100644 --- a/stacks/README.md +++ b/stacks/README.md @@ -3,3 +3,9 @@ ### C or C++ 1. [Balanced Parenthesis](c-or-cpp/balanced-parenthesis.cpp) + +### Java + +1. [Stack Implementation](Java/stack.java) +2. [Balanced Parenthesis](Java/Balanced_Paranthesis.java) +3. [Stock Span Problem](Java/The_Stock_Span_Problem.java) diff --git a/stacks/java/balanced-paranthesis.java b/stacks/java/balanced-paranthesis.java new file mode 100644 index 00000000..386c9d70 --- /dev/null +++ b/stacks/java/balanced-paranthesis.java @@ -0,0 +1,69 @@ + + +import java.util.Iterator; +import java.util.Stack; +import java.util.Vector; + +class Balanced_Paranthesis { + + public void problem(String input_string){ + + Vector output = new Vector<>(); + + Stack sta = new Stack<>(); + + for(int i = 0;i < input_string.length();i++){ + for (int j = 0; j <= i; j++){ + String sub_s = input_string.substring(j,i+1); + char[] sub = sub_s.toCharArray(); + int no_of_left_paranthesis = 0; + int no_of_right_paranthesis = 0; + for (int k = 0;k < sub_s.length();k++){ + if(sub[k] == '('){ + no_of_left_paranthesis = no_of_left_paranthesis + 1; + sta.push('('); + } + if(sub[k] == ')'){ + no_of_right_paranthesis = no_of_right_paranthesis + 1; + sta.pop(); + } + + if (sub_s.length() % 2 == 0 && no_of_left_paranthesis == no_of_right_paranthesis && sta.isEmpty()){ + output.add(sub_s); + } + else{ + while (!sta.isEmpty()){ + sta.pop(); + } + } + } + } + + int length = 0; + if (output.size() == 0){ + System.out.println(length + " "); + } + else{ + int max_len = 0; + String long_Str = ""; + + for(int s = 0;s < output.size();s++){ + if (output.get(s).length() > max_len){ + max_len = output.get(s).length(); + long_Str = output.get(s); + } + } + System.out.println("Maximum length of string : " + max_len + "and the string is : " + long_Str); + } + } + } +} + +class Main { + + public static void main(String[] args){ + Balanced_Paranthesis bal = new Balanced_Paranthesis(); + bal.problem("((()))"); + } +} + diff --git a/stacks/java/stack.java b/stacks/java/stack.java new file mode 100644 index 00000000..bf884e76 --- /dev/null +++ b/stacks/java/stack.java @@ -0,0 +1,93 @@ + + + class stack { + public int Max; + public int Top; + public int[] stack; + + public stack(int Max){ + this.Max = Max; + stack = new int[Max]; + Top = -1; + } + + /** + * if the stack is empty then always the top will be -1 + * so this should return a boolean if value of top < 1 then returns true which means + * stack is empty. + */ + public boolean isEmpty(){ + return (Top < 0); + } + + public int size(){ + return (Top+1); + } + + public void push(int x){ + if (size() >= Max){ + System.out.println("Stack Overflow"); + } + else{ + Top = Top + 1; + stack[Top] = x; + } + } + + /** + * pop function pops out the top element in the queue + * returns the element that is popped out of the queue + */ + public int pop(){ + if (size() == 0){ + System.out.println("Stack Empty Exception"); + } + else { + /** + * the top element in the stack will be popped out from the stack + */ + int popped_element = stack[Top]; + Top = Top - 1; + return popped_element; + } + return -1; + } + + public int top(){ + if(isEmpty()){ + System.out.println("Stack Empty Exception"); + } + else{ + return stack[Top]; + } + return -1; + } + + public void PrintStack(){ + if (isEmpty()){ + System.out.println("Empty"); + } + else{ + for(int i = 0;i < size();i++){ + System.out.print(stack[i] + " "); + } + System.out.println(); + } + } + +} +class Main { + + public static void main(String[] args){ + max = 1000; + stack object = new stack(max); + object.push(1); + object.push(2); + object.push(3); + object.pop(); + object.top(); + object.PrintStack(); + } +} + + diff --git a/stacks/java/the-stock-span-problem.java b/stacks/java/the-stock-span-problem.java new file mode 100644 index 00000000..7ee5eba0 --- /dev/null +++ b/stacks/java/the-stock-span-problem.java @@ -0,0 +1,98 @@ + + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Stack ; + +/** + * Problem statement : + * The stock span problem is a financial problem where we have a series of n daily price quotes for a stock + * and we need to calculate span of stock’s price for all n days. + * The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, + * for which the price of the stock on + * the current day is less than or equal to its price on the given day. + * For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding + * 7 days are {1, 1, 1, 2, 1, 4, 6} + */ +class The_Stock_Span_Problem extends stack{ + + public The_Stock_Span_Problem(int Max) { + super(Max); + } + + /** + * this problem is being solved with stack abstract data type. + */ + public void problem(int[] price,int n,int[] span){ + stack st = new stack(Max); + st.push(0); + + //span of the first day is always going to be 1 + span[0] = 1; + + + for (int i = 1;i < n;i ++){ + span[i] = 1; + st.PrintStack(); + while(!st.isEmpty() && price[st.top()] <= price[i]){ + st.pop(); + } + + if (st.isEmpty()){ + span[i] = i + 1; + } + else{ + span[i] = i - st.top(); + } + st.push(i); + } + printArray(span); + + } + + public void printArray(int[] span){ + System.out.println("The span of the stock array :" + Arrays.toString(span)); + } + + /** + * This is a different approach that has slightly higher time complexity than the approach used above + * Both the methods are awesome and star struck but...xD + */ + public void alternate_approach(int[] price,int n,int[] span){ + + Stack sta = new Stack<>(); + + span[0] = 1; + + for (int i = 1;i < n;i++){ + for (int j = 0;j < i;j++){ + sta.push(j); + } + + while(!sta.isEmpty() && price[sta.peek()] <= price[i]){ + sta.pop(); + } + if (sta.isEmpty()){ + span[i] = i + 1; + } + else{ + span[i] = i - sta.peek(); + } + sta.clear(); + } + printArray(span); + } +} + +class Main { + + //to test the span stock problem + public static void main(String[] args){ + int[] price = { 10, 4, 5, 90, 120, 80 }; + int n = price.length; + int[] span = new int[n]; + + The_Stock_Span_Problem prob = new The_Stock_Span_Problem(1000); + prob.alternate_approach(price,n,span); + } +}