- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in C++
Concept
With respect of given two strings S1 and S2 of equal lengths, our task is to determine an index i such that S1[0…i] and S2[i+1…n-1] give a palindrome when concatenated together. Ithas been seen that if it is not possible to determine such an index then print -1.
Input
S1 = “pqrsu”, S2 = “wxyqp”
Output
1
S1[0..1] = “pq”, S2[2..n-1] = “ypq”
S1 + S2 = “pqyqp” indicates is a palindrome.
Input
S1 = “pqrst”, S2 = “qprqz”
Output
-1
Method
- At first, we iterate from 0 to n (length of the string) and copy ith character from S1 to another string (assume it is S).
- After that we take another temporary string temp and copy the characters of S2 from index i +1 to n.
- Finally, we verify whether the string (S + temp) is palindrome or not.
Example
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Shows function that returns true if s is palindrome bool isPalindrome(string str){ int i = 0; int b = str.length() - 1; while (i< b) { if (str[i] != str[b]) return false; i++; b--; } return true; } // Shows function to return the required index int getIndex1(string S1, string S2, int n){ string S = ""; for (int i = 0; i< n; a++) { // Used to copy the ith character in S S = S + S1[i]; string temp = ""; // Used to copy all the character of string s2 in Temp for (int b = i + 1; b < n; b++) temp += S2[b]; // Verify whether the string is palindrome if (isPalindrome(S + temp)) { return i; } } return -1; } // Driver code int main(){ string S1 = "pqrsu", S2 = "wxyqp"; int n = S1.length(); cout << getIndex1(S1, S2, n); return 0; }
Output
1
- Related Articles
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python
- Program to find longest prefix that is also a suffix in C++
- match_results prefix() and suffix() in C++
- Program to find number of ways we can arrange letters such that each prefix and suffix have more Bs than As in Python
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two in C++
- Rearrange an array such that arr[i] = i in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- How can I eradicate some specific suffix or prefix or both from a MySQL string?
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K in C++
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Check if suffix and prefix of a string are palindromes in Python
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix in C++
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++

Advertisements