检查所有回文子串是否都是奇数长度
给定一个字符串's' 检查它的所有回文子字符串是否都是奇数长度。如果是,则打印“YES”或“NO”,否则打印。
例子:
Input: str = “geeksforgeeks”
Output: NO
Since, “ee” is a palindromic sub-string of even length.
Input: str = “madamimadam”
Output: YES
蛮力方法:
简单地说,遍历 's' 的每个子字符串并检查它是否是回文。如果它是一个回文,那么它必须是奇数长度。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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[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
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
Python
# 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
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that checks whether s
// contains a even length palindromic
// sub-strings or not.
bool CheckEven(string s)
{
for (int i = 1; i < s.size(); ++i) {
if (s[i] == s[i - 1]) {
return true;
}
}
return false;
}
// Driver code
int main()
{
string s = "geeksforgeeks";
if(CheckEven(s)==false)
cout<<("YES");
else
cout<<("NO");
}
// This code is contributed by
// Aditya Jaiswal
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
Javascript
输出
NO
有效方法:要检查 s 的所有回文子串是否都具有奇数长度,我们可以搜索它的偶数长度回文子串。我们知道每个偶数长度的回文至少有两个相同的连续字符(例如 cxxa、ee)。因此,我们可以一次检查两个连续的字符,看它们是否相同。如果是这样,那么 s 有一个偶数长度的回文子串,因此输出将为 NO,如果我们发现没有偶数长度的子串,答案将为 YES。
我们可以在一个字符串遍历之后完成这个检查。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that checks whether s
// contains a even length palindromic
// sub-strings or not.
bool CheckEven(string s)
{
for (int i = 1; i < s.size(); ++i) {
if (s[i] == s[i - 1]) {
return true;
}
}
return false;
}
// Driver code
int main()
{
string s = "geeksforgeeks";
if(CheckEven(s)==false)
cout<<("YES");
else
cout<<("NO");
}
// This code is contributed by
// Aditya Jaiswal
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
Javascript
输出:
NO
时间复杂度: O(N)
空间复杂度: O(1)