Day 13 - Java

Palindrome Extractor

You are given a list of sentences. Your task is to extract all unique palindromic words from these sentences and return them in lexicographical order.

A palindromic word is a word that reads the same backward as forward.

Complete the function extractPalindromes in the provided Java code. The function should take an array of strings, where each string represents a sentence, and return a list of unique palindromic words extracted from these sentences in lexicographical order.

Function Signature

public static List<String> extractPalindromes(String[] sentences)

Constraints

The input sentences will contain only lowercase English letters and spaces.

The length of each sentence will not exceed 1000 characters.

The number of sentences will not exceed 1000.

Each word in a sentence is separated by exactly one space.

Example

Input

Madam Arora teaches Malayalam and Anna drives a civic racecar

Output

ANNA ARORA CIVIC MADAM MALAYALAM RACECAR 

Explanation

After extracting palindromic words from the sentence, the unique palindromic words are: "Madam", "Arora", "Malayalam", "Anna", "civic", "racecar"

The output displays these words separated by spaces in upper case in alphabetical order.

Note

You don't need to worry about handling punctuation or special characters.


SOLUTION:

import java.util.*;

public class PalindromeExtractor {

    public static boolean isPalindrome(String word) {

        return word.equals(new StringBuilder(word).reverse().toString());

    }

    public static List<String> extractPalindromes(String[] sentences) {

        Set<String> palindromeSet = new TreeSet<>();

        for (String s : sentences) {

            String[] words = s.split("\\s+");

            for (String w : words) {

                if (isPalindrome(w.toLowerCase())) {

                  if(w.length() > 1)

                    palindromeSet.add(w.toUpperCase());

                }

            }

        }

        return new ArrayList<>(palindromeSet);

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String[] words = scanner.nextLine().split("\\s+");

        List<String> result = extractPalindromes(words);

        for(String s: result)

        {

          System.out.print(s + " ");

        }

        scanner.close();

    }

}


Insights:
  • The isPalindrome method efficiently checks whether a given string is a palindrome by comparing it with its reverse. This method utilizes the StringBuilder class to reverse the string.
  • The extractPalindromes method takes an array of sentences as input and extracts all unique palindromic words from these sentences. It converts each word to lowercase before checking for palindromes to ensure case insensitivity.
  • The palindromeSet variable, of type Set<String>, is used to store unique palindromic words. It helps to automatically eliminate duplicate entries.
  • The palindromeSet is instantiated as a TreeSet, which automatically sorts its elements in natural order (lexicographical order for strings).
  • The split("\\s+") method is used to split each sentence into words based on whitespace. This allows processing individual words for palindrome checking.
  • The code includes a condition if (w.length() > 1) to filter out single-character palindromes. This ensures that only meaningful palindromic words are added to the result set.
  • Palindromic words are converted to uppercase before adding them to the set. This ensures that all palindromes in the output are displayed in uppercase as space separated strings.
Wishing a RACECAR weekend ahead! 🏁 #Palindrome :)

Comments