Skip to content

Instantly share code, notes, and snippets.

@danieldietrich
Created August 21, 2017 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldietrich/dfedf1288f2a3bb9cf5b5e048e6429bf to your computer and use it in GitHub Desktop.
Save danieldietrich/dfedf1288f2a3bb9cf5b5e048e6429bf to your computer and use it in GitHub Desktop.
Lukas' palindrome challenge solved using Vavr
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