Java Program to Print Smallest and Biggest Possible Palindrome Word in a Given String Last Updated : 04 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report A Palindrome String is a string whose reversed string is equal to the original string. In this Java Program, we are going to see the approach of printing the smallest and biggest palindrome word in a String. Given a sentence of words in the form of a string, and we need to print the smallest and longest palindromic word out of those words. Example:Input: Wow madam is driving racecar. Smallest Palindrome: Wow Longest Palindrome: racecar Explanation: The string contains three palindrome words (i.e., madam, Wow, racecar) but the length of racecar is greatest and that of wow is smallest. Input: "Nitin is a good guy" Smallest Palindrome: a Longest Palindrome: Nitin Approach: lengthPalindrome() function finds the longest and smallest palindrome word by extracting every word of the string and passing it to checkPalin() function and then updating the answer string for the smallest and longest string by comparing it with the length of the original string if it is a palindrome. checkPalin() function checks if the word is palindrome. It returns true if word is palindrome else returns false. It makes sure that empty strings are not counted as palindrome as the user may enter more than one spaces in between or at the beginning of the string Example: Java // Java program to print the smallest and // longest palindromic word out of the words of a sentence public class Main { // Function to check if a // word is palindrome public static boolean checkPalin(String word) { int n = word.length(); // making the check case // case insensitive word = word.toLowerCase(); // loop to check palindrome for (int i = 0; i < n; i++, n--) { if (word.charAt(i) != word.charAt(n - 1)) return false; } return true; } // Determine the smallest and biggest // palindromes in a given string public static void lengthPalindrome(int temp, String words[]) { int count = 0; String smallest = "", longest = ""; for (int i = 0; i < temp; i++) { if (checkPalin(words[i])) { count++; // Initialize smallest and longest // when first palindromic word // is found if (count == 1) smallest = longest = words[i]; // Compare smallest and longest with each // palindromic words else { // If length of smallest is greater // than next palindromic word then // Store that word in smallest if (smallest.length() > words[i].length()) smallest = words[i]; // If length of longest is less // than next palindromic word then // Store that word in longest if (longest.length() < words[i].length()) longest = words[i]; } } } if (count == 0) System.out.println("No palindrome found"); else { System.out.println("Smallest palindrome: " + smallest); System.out.println("Biggest palindrome: " + longest); } } public static void main(String[] args) { String string = "Wow Madam is driving racecar"; String word = ""; String[] words = new String[100]; int temp = 0; // Add extra space after string // to get the last word string = string + " "; for (int i = 0; i < string.length(); i++) { // Split the string into words if (string.charAt(i) != ' ') { word = word + string.charAt(i); } else { // Add word to array words words[temp] = word; temp++; word = ""; } } System.out.println("Inputted String : " + string); lengthPalindrome(temp, words); } } OutputInputted String : Wow Madam is driving racecar Smallest palindrome: Wow Biggest palindrome: racecar Comment More infoAdvertise with us Next Article Return a Palindromic String after removing minimum length Prefix from given String R rbbansal Follow Improve Article Tags : Java Java Programs Java-String-Programs Practice Tags : Java Similar Reads Java Program to Check Whether a String is a Palindrome A string in Java can be called a palindrome if we read it from forward or backward, it appears the same or in other words, we can say if we reverse a string and it is identical to the original string for example we have a string s = "jahaj " and when we reverse it s = "jahaj"(reversed) so they look 8 min read Java Program to Find All Palindromic Sub-Strings of a String Given a string, the task is to count all palindrome substring in a given string. Input : aba Output : 4 Explanation : All palindrome substring are : "aba" , "a" , "b", "a" Input : TENET Output : 7 Explanation : All palindrome sub-string are : "T" , "E" , "N", "E", "T" , "ENE" , "TENET" Approach: Tak 2 min read Find all palindromic sub-strings of a given string | Set 2 Given a string, the task is to find all the palindromic sub-strings from the given string.In Set - 1, another approach has been already discussed and that consider only distinct sub-strings but in this equal sub-strings i.e. ll and ll are considered as two sub-strings, not one. Examples: Input : hel 7 min read Return a Palindromic String after removing minimum length Prefix from given String Given a string B, the task is to find the minimum length prefix of the string which, when removed and added to the end of the string, will make the string a palindrome. Return the palindromic string. If no such string exists, we need to determine that. Examples: Input: "aabb"Output: "abba"Explanatio 5 min read Maximum even length sub-string that is permutation of a palindrome Given string str , the task is to find the maximum length of the sub-string of str that can be arranged into a Palindrome (i.e at least one of its permutation is a Palindrome). Note that the sub-string must be of even length. Examples: Input: str = "124565463" Output: 6 "456546" is the valid sub-str 9 min read Longest Palindromic Substring using hashing in O(nlogn) Given a string S, The task is to find the longest substring which is a palindrome using hashing in O(N log N) time. Input: S: âforgeeksskeegforâ, Output: âgeeksskeegâ Input: S: âGeeksâ, Output: âeeâ Hashing to Solve the Problem:The hashing approach to solving the longest palindromic substring proble 11 min read Like