Last active
September 13, 2016 18:46
-
-
Save StevenACoffman/4bf5c43fc1102b1a76cb751f5f9773c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.AbstractMap.SimpleEntry; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
class Balance { | |
String hello = "hello"; | |
List<String> beginnings = Arrays.asList("[", "(", "{"); | |
Map<String, String> endingsToBeginnings = Collections.unmodifiableMap(Stream.of( | |
new SimpleEntry<>("]", "["), | |
new SimpleEntry<>(")", "("), | |
new SimpleEntry<>("}", "{")) | |
.collect(Collectors | |
.toMap(Map.Entry::getKey, Map.Entry::getValue))); | |
ArrayDeque<String> stack = new ArrayDeque<>(); | |
public String notMatching = "()]".chars() | |
.mapToObj(i -> Character.toString((char) i)).filter(letter -> { | |
if (beginnings.contains(letter)) { | |
stack.push(letter); | |
return true; | |
} else if (endingsToBeginnings.containsKey(letter)) { | |
String expectedPunctuation = endingsToBeginnings.get(letter); | |
String actualPunctuation = stack.peek(); | |
if (expectedPunctuation.equals(actualPunctuation)) { | |
stack.pop(); | |
return true; | |
} | |
} | |
return false; | |
} | |
).findFirst().get(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment