Open In App

Check if String formed by first and last X characters of a String is a Palindrome

Last Updated : 08 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string str and an integer X. The task is to find whether the first X characters of both string str and reversed string str are same or not. If it is equal then print true, otherwise print false.

Examples:

Input: str = abcdefba, X = 2
Output: true
Explanation
First 2 characters of both string str and reversed string str are same.

Input: str = GeeksforGeeks, X = 3
Output: false

Approach: This problem can be solved by iterating over the characters of the string str. Follow the steps below to solve this problem: 

  • Initialize two variables say, i as 0 and n as length of str to store position of current character and length of the string str respectively.
  • Iterate while i less than n and x:
    • If ith character from starting and ith from the last are not equal, then print false and return.
  • After completing the above steps, print true as the answer.

Below is the implementation of the above approach : 

C++




// C++ implementation for the above approach
#include <bits/stdc++.h>
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




<script>
  
// JavaScript 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
function isEqualSubstring(str, x)
{
    // Length of the string str
    let n = str.length;
    let 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]) {
            document.write("false");
            return;
        }
        i++;
    }
 
    // Finally, print true
    document.write("true");
}
 
// Driver Code
 
    // Given Input
    let str = "GeeksforGeeks";
    let x = 3;
 
    // Function Call
    isEqualSubstring(str, x);
 
 
 
    // This code is contributed by Potta Lokesh
   
    </script>


Output

false

Time complexity: O(min(n, k))
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads