Sunday, January 12, 2025
Google search engine
HomeData Modelling & AICheck if String formed by first and last X characters of a...

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

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 = neveropen, 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 = "neveropen";
    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 = "neveropen";
        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 = "neveropen"
    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 = "neveropen";
    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 = "neveropen";
    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)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments