📌  相关文章
📜  检查由字符串的第一个和最后一个 X字符组成的字符串是否是回文

📅  最后修改于: 2022-05-13 01:57:07.284000             🧑  作者: Mango

检查由字符串的第一个和最后一个 X字符组成的字符串是否是回文

给定一个字符串str和一个整数X 。任务是查找字符串str和反转字符串str的前X个字符是否相同。如果相等则打印true ,否则打印false

例子:

方法:这个问题可以通过遍历字符串str的字符来解决。请按照以下步骤解决此问题:

  • 初始化两个变量, i0nstr的长度,分别存储当前字符的位置和字符串str的长度。
  • i小于nx时迭代:
    • 如果开始的第i 个字符和最后一个字符的第 i 个不相等,则打印false返回
  • 完成上述步骤后,打印true作为答案。

以下是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to check whether the first
// x characters of both string str and
// reversed string str are same or not
void isEqualSubstring(string str, int x)
{
    // Length of the string str
    int n = str.length();
    int i = 0;
 
    // Traverse over the string while
    // first and last x characters are
    // not equal
    while (i < n && i < x) {
 
        // If the current and n-k-1 from last
        // character are not equal
        if (str[i] != str[n - i - 1]) {
            cout << "false";
            return;
        }
        i++;
    }
 
    // Finally, print true
    cout << "true";
}
 
// Driver Code
int main()
{
    // Given Input
    string str = "GeeksforGeeks";
    int x = 3;
 
    // Function Call
    isEqualSubstring(str, x);
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
    // Function to check whether the first
    // x characters of both string str and
    // reversed string str are same or not
    public static void isEqualSubstring(String str, int x)
    {
        // Length of the string str
        int n = str.length();
        int i = 0;
 
        // Traverse over the string while
        // first and last x characters are
        // not equal
        while (i < n && i < x) {
 
            // If the current and n-k-1 from last
            // character are not equal
            if (str.charAt(i) != str.charAt(n - i - 1)) {
                System.out.println("false");
                return;
            }
            i++;
        }
 
        // Finally, print true
        System.out.println("true");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        String str = "GeeksforGeeks";
        int x = 3;
 
        // Function Call
        isEqualSubstring(str, x);
    }
}


Python3
# Python3 program for the above approach
 
# Function to check whether the first
# x characters of both string str and
# reversed string str are same or not
def isEqualSubstring(string, x):
   
    # Length of the string str
    n = len(string)
    i = 0
     
    # Traverse over the string while
    # first and last x characters are
    # not equal
    while i < n and i < x:
         
        # If the current and n-k-1 from last
        # character are not equal
        if (string[i] != string[n-i-1]):
            print("false")
            return
         
        i += 1
         
    # Finally, print true
    print("true")
    return
 
# Driver Code
if __name__ == '__main__':
     
    # Given input
    string = "GeeksforGeeks"
    x = 3
 
    # Function Call
    isEqualSubstring(string, x)
 
# This code is contributed by MuskanKalra1


C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check whether the first
// x characters of both string str and
// reversed string str are same or not
static void isEqualSubstring(string str, int x)
{
   
    // Length of the string str
    int n = str.Length;
    int i = 0;
 
    // Traverse over the string while
    // first and last x characters are
    // not equal
    while (i < n && i < x) {
 
        // If the current and n-k-1 from last
        // character are not equal
        if (str[i] != str[n - i - 1]) {
            Console.Write("false");
            return;
        }
        i++;
    }
 
    // Finally, print true
    Console.Write("true");
}
 
// Driver Code
public static void Main()
{
   
    // Given Input
    string str = "GeeksforGeeks";
    int x = 3;
 
    // Function Call
    isEqualSubstring(str, x);
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出
false

时间复杂度: O(min(n, k))
辅助空间: O(1)