📌  相关文章
📜  检查给定的字符串是否是回文奇数

📅  最后修改于: 2021-04-29 16:05:42             🧑  作者: Mango

给定字符串str ,任务是检查str的奇数索引处的字符是否形成回文字符串。如果不是,则打印“否”,否则打印“是”
例子:

原始的方法:简易方法是通过附加指定的字符串的奇数索引字符来创建新的字符串。然后,只需检查形成的字符串是否回文。如果字符串是回文,则打印“是”,否则打印“否”
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to check if the string str
// is palindromic or not
bool isPalindrome(string str)
{
  
    // Iterate the string str from left
    // and right pointers
    int l = 0;
    int h = str.size() - 1;
  
    // Keep comparing characters
    // while they are same
    while (h > l) {
  
        // If they are not same
        // then return false
        if (str[l++] != str[h--]) {
            return false;
        }
    }
  
    // Return true if the string is
    // palindromic
    return true;
}
  
// Function to make string using odd
// indices of string str
string makeOddString(string str)
{
    string odd = "";
    for (int i = 1; i < str.size();
         i += 2) {
        odd += str[i];
    }
  
    return odd;
}
  
// Functions checks if characters at
// odd index of the string forms
// palindrome or not
void checkOddlyPalindrome(string str)
{
  
    // Make odd indexed string
    string odd = makeOddString(str);
  
    // Check for Palindrome
    if (isPalindrome(odd))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
  
// Driver Code
int main()
{
    // Given string
    string str = "ddwfefwde";
  
    // Function Call
    checkOddlyPalindrome(str);
    return 0;
}


Java
// Java program for the above approach 
class GFG{
      
// Function to check if the String str
// is palindromic or not
public static boolean isPalindrome(String str)
{
      
    // Iterate the String str from left
    // and right pointers
    int l = 0;
    int h = str.length() - 1;
  
    // Keep comparing characters
    // while they are same
    while (h > l)
    {
          
        // If they are not same
        // then return false
        if (str.charAt(l) != str.charAt(h))
        {
            return false;
        }
          
        l++;
        h--;
    }
  
    // Return true if the String is
    // palindromic
    return true;
}
  
// Function to make String using odd
// indices of String str
public static String makeOddString(String str)
{
    String odd = "";
      
    for(int i = 1; i < str.length(); i += 2)
    {
       odd += str.charAt(i);
    }
    return odd;
}
  
// Functions checks if characters at
// odd index of the String forms
// palindrome or not
public static void checkOddlyPalindrome(String str)
{
  
    // Make odd indexed String
    String odd = makeOddString(str);
  
    // Check for Palindrome
    if (isPalindrome(odd))
        System.out.println("Yes");
    else
        System.out.println("No");
}
  
// Driver Code
public static void main(String []args)
{
      
    // Given String
    String str = "ddwfefwde";
  
    // Function Call
    checkOddlyPalindrome(str);
}
}
  
// This code is contributed by grand_master


Python3
# Python3 program for the above approach 
  
# Function to check if the string  
# str is palindromic or not 
def isPalindrome(str):
  
    # Iterate the string str from 
    # left and right pointers 
    l = 0; 
    h = len(str) - 1; 
  
    # Keep comparing characters 
    # while they are same 
    while (h > l): 
  
        # If they are not same 
        # then return false 
        if (str[l] != str[h]): 
            return False;
              
        l += 1
        h -= 1
          
    # Return true if the string is 
    # palindromic
    return True; 
  
# Function to make string using odd 
# indices of string str 
def makeOddString(str):
  
    odd = ""; 
    for i in range(1, len(str), 2):
        odd += str[i]; 
      
    return odd; 
  
# Functions checks if characters at 
# odd index of the string forms 
# palindrome or not 
def checkOddlyPalindrome(str):
  
    # Make odd indexed string 
    odd = makeOddString(str); 
  
    # Check for Palindrome 
    if (isPalindrome(odd)):
        print("Yes") 
    else:
        print("No")
  
# Driver code
  
# Given string
str = "ddwfefwde";
  
# Function call
checkOddlyPalindrome(str); 
  
# This code is contributed by grand_master


C#
// C# program for the above approach 
using System;
  
class GFG{
      
// Function to check if the String str 
// is palindromic or not 
static bool isPalindrome(string str) 
{ 
      
    // Iterate the String str from left 
    // and right pointers 
    int l = 0; 
    int h = str.Length - 1; 
  
    // Keep comparing characters 
    // while they are same 
    while (h > l) 
    { 
          
        // If they are not same 
        // then return false 
        if (str[l] != str[h]) 
        { 
            return false; 
        } 
          
        l++; 
        h--; 
    } 
  
    // Return true if the String is 
    // palindromic 
    return true; 
} 
  
// Function to make String using odd 
// indices of String str 
static string makeOddString(string str) 
{ 
    string odd = ""; 
      
    for(int i = 1; i < str.Length; i += 2) 
    { 
        odd += str[i]; 
    } 
    return odd; 
} 
  
// Functions checks if characters at 
// odd index of the String forms 
// palindrome or not 
static void checkOddlyPalindrome(string str) 
{ 
  
    // Make odd indexed String 
    string odd = makeOddString(str); 
  
    // Check for Palindrome 
    if (isPalindrome(odd)) 
        Console.WriteLine("Yes"); 
    else
        Console.WriteLine("No"); 
} 
  
// Driver code
static void Main()
{
      
    // Given String 
    string str = "ddwfefwde"; 
      
    // Function Call 
    checkOddlyPalindrome(str); 
}
}
  
// This code is contributed by divyeshrabadiya07


输出:
Yes

时间复杂度: O(N) ,N是字符串的长度。
辅助空间: O(N / 2)