Skip to content

Commit

Permalink
#2 - lambda consumer stack
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Feb 12, 2024
1 parent 2209f26 commit a885f7d
Showing 1 changed file with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,56 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class Stack {
public class StackImpl {

String pushKeys = "[{(";
String pullKeys = "]})";
// move these
boolean balanced = true;
Stack<String> stack = new Stack<String>();
/*
* Using streams and stack from java 8
*
*/
boolean isBalanced(String s) {
// stream and check push/pull pairs
s.chars().mapToObj(c -> (char)c).forEach(mapConsumer);
if(!stack.isEmpty())
return false;
else
return balanced;
}

Consumer<Character> mapConsumer = new Consumer<Character>() {
public void accept(Character s) {
if(pushKeys.contains(s.toString())) {
stack.push(s.toString());
} else {
if(!stack.isEmpty()) { // check for too many closing brackets
String y = stack.pop();
// if pull of last push corresponds to current char - ok
if(pushKeys.indexOf(y) != pullKeys.indexOf(s.toString())) {
balanced = false;
}
} else {
balanced = false;
}
}
}
};

/**
* @deprecated
* Using mix of pre-post Java 8
* @param s
* @return
*/
boolean isBalanced2(String s) {
boolean balanced = true;
// index is important
String pushKeys = "[{(";
String pullKeys = "]})";
Map<String, Integer> dictionaryIndex = new HashMap<String, Integer>();
Expand Down Expand Up @@ -63,8 +106,11 @@ boolean isBalanced(String s) {
}

public static void main(String[] args) {
Stack stack = new Stack();
String aString = "{}([()][])";
StackImpl stack = new StackImpl();
String aString = "{}([()][])"; // true
//String aString = "{}([()][]"; // false
//String aString = "{}([()][]))"; // false
//String aString = "{[}]"; // false - need to check pull removal
System.out.println(stack.isBalanced(aString));
}
}

0 comments on commit a885f7d

Please sign in to comment.