Created
August 21, 2017 23:57
-
-
Save danieldietrich/dfedf1288f2a3bb9cf5b5e048e6429bf to your computer and use it in GitHub Desktop.
Lukas' palindrome challenge solved using Vavr
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
Seq<Tuple<Integer, Integer>> getSubstringPalindromes(String s) { | |
final N = s.length(); | |
return List.range(0, N-1) | |
.flatMap(i -> List.of(Tuple.of(i, i+1), Tuple.of(i, i+2))) | |
.flatMap(t -> List.unfold(t, tt -> { | |
final int i = tt._1; | |
final int j = tt._2; | |
return i >= 0 && j <= N && isPalindrome(s.substring(i, j)) | |
? Option.some(Tuple(i-1, j+1), tt) | |
: Option.none(); | |
})); | |
} | |
boolean isPalindrome(String string) { | |
final gnirts = new StringBuilder(string).reverse().toString(); | |
return string.equals(gnirts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment