Check if all the palindromic sub-strings are of odd length
Given a string ‘s’ check if all of its palindromic sub-strings are of odd length or not. If yes then print “YES” or “NO” otherwise.
Examples:
Input: str = “geeksforgeeks”
Output: NO
Since, “ee” is a palindromic sub-string of even length.Input: str = “madamimadam”
Output: YES
Approach: Simply, iterate over each sub-string of ‘s’ and check if it is a palindrome. If it is a palindrome then it must of odd length.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include<bits//stdc++.h> using namespace std; // Function to check if // the string is palindrome bool checkPalindrome(string s) { for (int i = 0; i < s.length(); i++) { if(s[i] != s[s.length() - i - 1]) return false; } return true; } // Function that checks whether // all the palindromic // sub-strings are of odd length. bool CheckOdd(string s) { int n = s.length(); for (int i = 0; i < n; i++) { // Creating each substring string x = ""; for (int j = i; j < n; j++) { x += s[i]; // If the sub-string is // of even length and // is a palindrome then, // we return False if(x.length() % 2 == 0 && checkPalindrome(x) == true) return false; } } return true; } // Driver code int main() { string s = "geeksforgeeks"; if(CheckOdd(s)) cout<<("YES"); else cout<<("NO"); } // This code is contributed by // Sahil_shelangia |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to check if // the string is palindrome static boolean checkPalindrome(String s) { for (int i = 0; i < s.length(); i++) { if(s.charAt(i) != s.charAt(s.length() - i - 1)) return false; } return true; } // Function that checks whether // all the palindromic // sub-strings are of odd length. static boolean CheckOdd(String s) { int n = s.length(); for (int i = 0; i < n; i++) { // Creating each substring String x = ""; for (int j = i; j < n; j++) { x += s.charAt(j); // If the sub-string is // of even length and // is a palindrome then, // we return False if(x.length() % 2 == 0 && checkPalindrome(x) == true) return false; } } return true; } // Driver code public static void main(String args[]) { String s = "geeksforgeeks"; if(CheckOdd(s)) System.out.print("YES"); else System.out.print("NO"); } } // This code is contributed // by Arnab Kundu |
Python3
# Python implementation of the approach # Function to check if # the string is palindrome def checkPalindrome(s): for i in range(len(s)): if(s[i] != s[len(s)-i-1]): return False return True # Function that checks whether # all the palindromic # sub-strings are of odd length. def CheckOdd(s): n = len(s) for i in range(n): # Creating each substring x = "" for j in range(i, n): x += s[j] # If the sub-string is # of even length and # is a palindrome then, # we return False if(len(x)% 2 == 0 and checkPalindrome(x) == True): return False return True # Driver code s = "geeksforgeeks"if(CheckOdd(s)): print("YES") else: print("NO") |
C#
// C# implementation of the approach using System; public class GFG { // Function to check if // the string is palindrome static bool checkPalindrome(String s) { for (int i = 0; i < s.Length; i++) { if(s[i] != s[(s.Length - i - 1)]) return false; } return true; } // Function that checks whether // all the palindromic // sub-strings are of odd length. static bool CheckOdd(String s) { int n = s.Length; for (int i = 0; i < n; i++) { // Creating each substring String x = ""; for (int j = i; j < n; j++) { x += s[j]; // If the sub-string is // of even length and // is a palindrome then, // we return False if(x.Length % 2 == 0 && checkPalindrome(x) == true) return false; } } return true; } // Driver code public static void Main() { String s = "geeksforgeeks"; if(CheckOdd(s)) Console.Write("YES"); else Console.Write("NO"); } } /* This code is contributed by 29AjayKumar*/ |
PHP
NO
Recommended Posts:
- Count all Prime Length Palindromic Substrings
- Count of Palindromic substrings in an Index range
- Check if a string contains a palindromic sub-string of even length
- Rearrange the string to maximize the number of palindromic substrings
- Sum of all odd length palindromic numbers within the range [L, R]
- Number of palindromic subsequences of length k where k <= 3
- Convert all substrings of length 'k' from base 'b' to decimal
- Count number of distinct substrings of a given length
- Number of strings of length N with no palindromic sub string
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Maximum length palindromic substring such that it starts and ends with given char
- Make palindromic string non-palindromic by rearranging its letters
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Check if a binary string contains all permutations of length k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



