Perfect Reversible String in JavaScript
Last Updated :
29 Nov, 2023
A string is said to be a perfectly reversible string when the reverse of all the possible substrings of the string is available or present in the string. In this article, we are going to check whether a string is a perfectly reversible string or not in JavaScript with the help of practical implementation and code examples.
Examples:
Input: string = "xyz"
Output: "No"
Explanation: All possible sub strings: 'x', 'y', 'z', 'xy', 'yz', 'xyz'.
Here the reverse of sub strings 'xy', 'yz' and 'xyz' is not avaialable.
Hence, it returns No as output.
Input: string = "xyx"
Output: "Yes"
Explanation: All possible sub strings: 'x', 'y', 'x', 'xy', 'yx', 'xyx'.
Here the reverse of all the sub strings is available.
Hence, it returns Yes as output.
There are two approaches available to solve this question:
Naive Approach
In the naive or simple approach, we will first find out all the substrings of the provided string and store them in an array. After getting the substrings, we will check whether the reverse of all the substrings is the same as the substring or not, and according to the checked condition, the result will be shown to the user on the output screen.
Example: This code example will help you understand whether a string is perfectly reversible or not in JavaScript:
Javascript
function isRevPer(str) {
let subStr = [];
let n = str.length;
let count = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j <= n; j++) {
subStr.push(str.slice(i, j));
}
}
subStr.forEach((substring) => {
let subStringReverse = substring
.split( '' ).reverse().join( '' );
if (subStr.includes(subStringReverse)) {
count++;
}
})
if (count === subStr.length) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
}
isRevPer( "xyx" );
isRevPer( "xyz" );
isRevPer( "abab" );
isRevPer( "aba" );
|
Time Complexity: O(n^2), n is the length of the string.
Space Complexity: O(m), m is the length of substring array or number of sub strings.
Efficient Approach
In the efficient approach, we can simply check for the string whether it is a palindrome string or not. If the string is a palindrome, then there will be the reverse of all the substring also avaiable in the string. Otherwise, the reverse of all sub strings will not be present in the string and the string will not be a perfectly reversible string.
Example: The below example will explain the practical implementation of efficient approach to check for the perfect reversible string in JavaScript.
Javascript
function perRev(str) {
let start = 0;
let end = str.length - 1;
while (start < end) {
if (str[start] !== str[end]) {
return false ;
}
start++;
end--;
}
return true ;
}
perRev( "xyx" ) ?
console.log( 'Yes' ) : console.log( 'No' );
perRev( "xyz" ) ?
console.log( 'Yes' ) : console.log( 'No' );
perRev( "abab" ) ?
console.log( 'Yes' ) : console.log( 'No' );
perRev( "aba" ) ?
console.log( 'Yes' ) : console.log( 'No' );
|
Time Complexity: O(n), n is the length of string.
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...