
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check for Reversible Equal Substring at the Ends
In this problem, we need to find the reversible equal substring of maximum length from the start and end of the string.
The problem is very similar to finding the palindromic string. We can start traversing the string and traverse the string until characters from the start and end match.
Problem statement ? We have given string str containing N characters. We need to check whether the string contains the reversible equal substring at the start and end of the string. If we find the substring according to the given condition, print the longest substring. Otherwise, print ?false' in the output.
Sample examples
Input
str = "tutorialsotut"
Output
?tut'
Explanation ? The ?tut' is present at the start, and the reverse of ?tut' is present at the end.
Input
str = ?abcytrfghgyuyjcba'
Output
?abc'
Explanation ? In the given string, ?abc' is present at the start, and ?cab' is present at the end.
Input
str = ?vdfdvcf'
Output
False
Explanation ? The string doesn't contain any reversible substring. So, it prints false.
Approach 1
In this approach, we will start traversing the string from the start. We will match the characters of the string from start and end. When we find unmatching characters, we will stop iterations.
Algorithm
Step 1 ? Initialize the ?p' variable with 0 and the ?q' variable with the string length ? 1.
Step 2 ? Also, initialize the ?res' variable with an empty string to store the required string.
Step 3 ? Use the loop to make iterations until ?q' is greater than or equal to zero.
Step 4 ? If s[p] and s[q] characters are equal, append them to the ?res' string. Also, increase the p?value by 1 and decrease q by 1.
Step 5 ? If s[p] and s[q] are not the same, break the loop using the ?break' keyword.
Step 6 ? If the size of the ?res' string equals zero, print false. Otherwise, print the ?res' string.
Example
#include <bits/stdc++.h> using namespace std; // Check if the string has reversible equal substring at the start and end of a given string void ReversibleEqual(string s) { // Initialize the length and pointer variables int len = s.size(); int p = 0; int q = len - 1; string res = ""; // Iterate over the string while (q >= 0) { // If characters at index p and q are the same if (s[p] == s[q]) { // append a character to 'res', and modify p and q value res += s[p]; p++; q--; } else { break; } } // If the substring is not present, print False if (res.size() == 0) cout << "False"; // print true else { cout << "True \n" << res; } } int main() { string str = "tutorialsotut"; ReversibleEqual(str); return 0; }
Output
True tuto
Time complexity? O(N) as we use a loop to iterate the string
Space complexity ? O(N) as we store the reversible string.
Programmers can optimize the above solution by printing each character directly rather than appending it to the ?res' string. The solution approach is very similar to checking whether a string is a palindrome.