0% found this document useful (0 votes)
6 views7 pages

Java Stack Solutions for Algorithms

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Java Stack Solutions for Algorithms

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stack Solutions

keerthanaamujuri47@[Link]
Solution 1:

Time Complexity : o(n)


Space Complexity: o(n)

import [Link].*;

class Solution {
public static void main(String args[]){
Node one = new Node(1);
Node two = new Node(2);
Node three = new Node(3);
Node four = new Node(4);
Node five = new Node(3);
Node six = new Node(2);
Node seven = new Node(1);
[Link] = two;
[Link] = three;
[Link] = four;
[Link] = five;
[Link] = six;
[Link] = seven;
boolean condition = isPalindrome(one);
[Link]("Palindrome :" + condition);
}
static boolean isPalindrome(Node head){

Node slow = head;


boolean ispalin = true;
Stack<Integer> stack = new Stack<Integer>();

while (slow != null) {


[Link]([Link]);
slow = [Link];
}

while (head != null) {


int i = [Link]();
if ([Link] == i) {

keerthanaamujuri47@[Link]
ispalin = true;
}
else {
ispalin = false;
break;
}
head = [Link];
}
return ispalin;
}
}

class Node {
int data;
Node ptr;
Node(int d){
ptr = null;
data = d;
}
}

Solution 2 :

Time Complexity : o(n)


Space Complexity: o(1)

import [Link].*;
import [Link].*;

class Solution{
public static void main(String []args){
String str = new String("/a/./b/../../c/");
String res = simplify(str);
[Link](res);
}
static String simplify(String A){
Stack<String> st = new Stack<String>();
String res = "";

keerthanaamujuri47@[Link]
res += "/";
int len_A = [Link]();

for (int i = 0; i < len_A; i++){


String dir = "";
while (i < len_A && [Link](i) == '/')
i++;

while (i < len_A && [Link](i) != '/'){


dir += [Link](i);
i++;
}

if ([Link]("..") == true){
if (![Link]())
[Link]();
}

else if ([Link](".") == true)


continue;

else if ([Link]() != 0)
[Link](dir);
}

Stack<String> st1 = new Stack<String>();


while (![Link]()){

[Link]([Link]());
}

while (![Link]()){
if ([Link]() != 1)
res += ([Link]() + "/");
else
res += [Link]();
}
return res;
}

keerthanaamujuri47@[Link]
}

Solution 3 :

Time Complexity : o(n)


Space Complexity: o(n)

import [Link];

class Solution{
static String decode(String str){
Stack<Integer> integerstack = new Stack<>();
Stack<Character> stringstack = new Stack<>();
String temp = "", result = "";
for (int i = 0; i < [Link](); i++){
int count = 0;
if ([Link]([Link](i))){
while ([Link]([Link](i))){
count = count * 10 + [Link](i) - '0';
i++;
}

i--;
[Link](count);
}

else if ([Link](i) == ']'){


temp = "";
count = 0;

if (![Link]()){
count = [Link]();
[Link]();
}

while (![Link]() && [Link]()!='[' ){


temp = [Link]() + temp;
[Link]();
}

keerthanaamujuri47@[Link]
if (![Link]() && [Link]() == '[')
[Link]();

for (int j = 0; j < count; j++)


result = result + temp;

for (int j = 0; j < [Link](); j++)


[Link]([Link](j));

result = "";
}

else if ([Link](i) == '['){


if ([Link]([Link](i-1)))
[Link]([Link](i));

else{
[Link]([Link](i));
[Link](1);
}
}

else
[Link]([Link](i));
}

while (![Link]()){
result = [Link]() + result;
[Link]();
}
return result;
}

public static void main(String args[]){


String str = "3[b2[ca]]";
[Link](decode(str));
}
}

keerthanaamujuri47@[Link]
Solution 4 :

Time Complexity : o(n)


Space Complexity: o(n)

import [Link].*;
import [Link].*;

class Solution{

public static int maxWater(int[] height){


Stack<Integer> stack = new Stack<>();
int n = [Link];
int ans = 0;
for (int i = 0; i < n; i++) {
while ((![Link]())
&& (height[[Link]()] < height[i])) {
int pop_height = height[[Link]()];
[Link]();
if ([Link]())
break;
int distance = i - [Link]() - 1;
int min_height
= [Link](height[[Link]()],
height[i])
- pop_height;

ans += distance * min_height;


}
[Link](i);
}

return ans;
}

public static void main(String[] args){


int arr[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
[Link](maxWater(arr));
}
}

keerthanaamujuri47@[Link]

You might also like