Check if String formed by first and last X characters of a String is a Palindrome Last Updated : 08 Jul, 2021 Comments Improve Suggest changes Like Article Like 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 = 2Output: trueExplanation: First 2 characters of both string str and reversed string str are same. Input: str = GeeksforGeeks, X = 3Output: 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> Outputfalse Time complexity: O(min(n, k))Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if String formed by first and last X characters of a String is a Palindrome sharathmajjigi Follow Improve Article Tags : Strings DSA palindrome Practice Tags : palindromeStrings Similar Reads Check if the characters in a string form a Palindrome in O(1) extra space Given string str. The string may contain lower-case letters, special characters, digits, or even white spaces. The task is to check whether only the letters present in the string are forming a Palindromic combination or not without using any extra space. Note: It is not allowed to use extra space to 10 min read Longest Palindrome in a String formed by concatenating its prefix and suffix Given a string str consisting of lowercase English letters, the task is to find the longest palindromic string T which satisfies the following condition: T = p + m + s where p and s are the prefix and the suffix of the given string str respectively and the string m is either the prefix or suffix of 13 min read Check if a linked list of strings forms a palindrome Given a linked list handling string data, check to see whether data is palindrome or not? For example, Input : a -> bc -> d -> dcb -> a -> NULL Output : True String "abcddcba" is palindrome. Output : a -> bc -> d -> ba -> NULL Output : False String "abcdba" is not palindro 6 min read Check if K palindromic strings can be formed from a given string Given a string S of size N and an integer K, the task is to find whether the characters of the string can be arranged to make K palindromic strings simultaneously. Examples: Input: S = "annabelle", K = 2 Output: Yes Explanation: All characters of string S can be distributed into "elble" and "anna" w 7 min read Find if string is K-Palindrome or not using all characters exactly once Given a string str and an integer K, the task is to check if it is possible to make the string K palindromes using all the characters of the string exactly once.Examples: Input: str = "poor", K = 3 Output: Yes One way of getting 3 palindromes is: oo, p, rInput: str = "fun", K = 5 Output: No 2 palind 6 min read Check if a given string is a rotation of a palindrome Given a string, check if it is a rotation of a palindrome. For example your function should return true for "aab" as it is a rotation of "aba". Examples: Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any p 15+ min read Remove a character from a string to make it a palindrome Given a string, we need to check whether it is possible to make this string a palindrome after removing exactly one character from this. Examples: Input : str = âabcbaâ Output : Yes we can remove character âcâ to make string palindrome Input : str = âabcbeaâ Output : Yes we can remove character âeâ 10 min read Check if given string can be made Palindrome by removing only single type of character Given a string S, the task is to whether a string can be made palindrome after removing the occurrences of the same character, any number of times Examples: Input: S = "abczdzacb" Output: Yes Explanation: Remove first and second occurrence of character 'a', string S becomes "bczdzcb", which is a pal 7 min read Check if string remains palindrome after removing given number of characters Given a palindromic string str and an integer N. The task is to find if it is possible to remove exactly N characters from the given string such that the string remains a palindrome. Examples: Input: str = "abba", N = 1 Output: Yes Remove 'b' and the remaining string "aba" is still a palindrome. Inp 3 min read Check if given string can be made Palindrome by removing only single type of character | Set-2 Given a string S, the task is to whether a string can be made palindrome after removing the occurrences of the same character, any number of times Examples: Input: S = "abczdzacb"Output: YesExplanation: Remove first and second occurrence of character âaâ. String S becomes âbczdzcbâ, which is a palin 9 min read Like