|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.stream.IntStream; |
| 5 | + |
| 6 | +/** |
| 7 | + * 5. Longest Palindromic Substring |
| 8 | + * <p> |
| 9 | + * Medium |
| 10 | + * <p> |
| 11 | + * Given a string s, return the longest palindromic substring in s. |
| 12 | + * <p> |
| 13 | + * <p> |
| 14 | + * <p> |
| 15 | + * Example 1: |
| 16 | + * <p> |
| 17 | + * Input: s = "babad" |
| 18 | + * Output: "bab" |
| 19 | + * Explanation: "aba" is also a valid answer. |
| 20 | + * Example 2: |
| 21 | + * <p> |
| 22 | + * Input: s = "cbbd" |
| 23 | + * Output: "bb" |
| 24 | + * <p> |
| 25 | + * <p> |
| 26 | + * Constraints: |
| 27 | + * <p> |
| 28 | + * 1 <= s.length <= 1000 |
| 29 | + * s consist of only digits and English letters. |
| 30 | + */ |
| 31 | +public class LongestPalindromicSubstring { |
| 32 | + |
| 33 | + /** |
| 34 | + * Finds the longest substring which is a palindrome. |
| 35 | + * PROs: |
| 36 | + * - Very simple to algorithm to understand. |
| 37 | + * - Making use of Java Streams and parallelization. |
| 38 | + * - Even if theoretically the complexity is O(n^2), the algorithm will be pretty fast in practice because: |
| 39 | + * - each iteration of the algorithm is parallelized. |
| 40 | + * - at each isPalindrome check we iterate only string.length/2 but in many cases we return earlier if |
| 41 | + * invalid character is found |
| 42 | + * CONs: |
| 43 | + * - High complexity: O(1) + O(2) + ... + O(n) = (O(n) * O(n+1))/2 = O(n^2) |
| 44 | + * - Average in terms of performance(according to LeetCode). |
| 45 | + * Explanation: |
| 46 | + * As we want to find the longest substring, we will go from the longest substring to the smallest. |
| 47 | + * (0.) We create a stream of ints from 0 until input.length() to facilitate Stream parallelization. |
| 48 | + * 1. We generate all substrings of a string for a given length, where length ranges from @input.length -> 0 |
| 49 | + * We map each previous int to List<String> - generated substrings. |
| 50 | + * 2. We want to filter each list to find the palindromes, and we use findFirst just to find the first substring |
| 51 | + * of a given length, instead of finding any which will give us a different result each time(BUT CORRECT, FOR THE |
| 52 | + * GIVEN CONSTRAINTS!). Using findAny will increase performance, but will not pass LeetCode tests...(in real word, |
| 53 | + * we would have smarter tests than the current ones from LeetCode and we would use findAny, for these current constraints!) |
| 54 | + * To filter we will use the isPalindrome method which will: |
| 55 | + * - compare if string[0] == string[n-1], string[1] == string[n-2] ... |
| 56 | + * return immediately if the condition is not satisfied. |
| 57 | + * by doing this we are halving the number of iterations for each string check! |
| 58 | + * and we are also lowering the number of iterations by returning immediately. |
| 59 | + * 3. We will have now a Stream<Optional<String>>, because from each List<String> we took only the first |
| 60 | + * valid palindrome. It is an Optional, because some lists may not have any palindrome. For this, we want |
| 61 | + * to filter only the present ones. Only then, we can perform findFirst to find the longest one.(Because of how we ranged from 0 to input.length, where to 0 we mapped actually |
| 62 | + * the longest string) |
| 63 | + * 4. We unwrap the optionals and return the longest palindromic substring if exists or an empty string. |
| 64 | + * @param input string |
| 65 | + * @return longest palindrome string |
| 66 | + */ |
| 67 | + public String find(String input) { |
| 68 | + var longestSubstring = IntStream.range(0, input.length()) |
| 69 | + .parallel() |
| 70 | + .mapToObj(i -> generateSubstrings(input, input.length() - i)) |
| 71 | + .map(i -> i.parallelStream().filter(this::isPalindrome).findFirst()) |
| 72 | + .filter(Optional::isPresent) |
| 73 | + .findFirst(); |
| 74 | + |
| 75 | + return longestSubstring.orElse(Optional.empty()).orElse(""); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Generates all combination of substrings of length @length for an input @input string. |
| 80 | + * |
| 81 | + * @param input string |
| 82 | + * @param length length of substring to generate |
| 83 | + * @return a List of all combination of length @length based on the input string |
| 84 | + */ |
| 85 | + private List<String> generateSubstrings(String input, int length) { |
| 86 | + ArrayList<String> substrings = new ArrayList<>(); |
| 87 | + for (int i = 0; i <= input.length() - length; i++) { |
| 88 | + substrings.add( |
| 89 | + input.substring(i, length + i) |
| 90 | + ); |
| 91 | + } |
| 92 | + return substrings; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Checks if input string is a palindrome |
| 97 | + * |
| 98 | + * @param input test to be checked |
| 99 | + * @return true if string is palindrome or false otherwise |
| 100 | + */ |
| 101 | + private boolean isPalindrome(String input) { |
| 102 | + for (int i = 0, j = input.length() - 1; i < input.length() / 2 || j > input.length() / 2; i++, j--) { |
| 103 | + if (input.charAt(i) != input.charAt(j)) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + return true; |
| 108 | + } |
| 109 | +} |
0 commit comments