Perfect Reversible String in JavaScript
Last Updated :
19 Jul, 2024
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) {
// Creating the substrings array
let subStr = [];
let n = str.length;
let count = 0;
// Finding each substring of the string
for (let i = 0; i < n; i++) {
for (let j = i + 1; j <= n; j++) {
subStr.push(str.slice(i, j));
}
}
// Checking for the reverse is same or not
subStr.forEach((substring) => {
let subStringReverse = substring
.split('').reverse().join('');
if (subStr.includes(subStringReverse)) {
count++;
}
})
// Displaying results to the users
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;
// checking for palindrome string
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)
Checking Substrings for Reverse Existence
In this approach, we don't generate all the substrings explicitly. Instead, we iterate through each character in the string and check whether its reverse substring exists in the original string. If all substrings' reverses exist, the string is perfectly reversible.
Example:
JavaScript
function isRevPer(str) {
// Checking for each character whether its reverse exists
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j <= str.length; j++) {
let substring = str.substring(i, j);
let substringReverse = substring.split('').reverse().join('');
if (!str.includes(substringReverse)) {
return "No";
}
}
}
return "Yes";
}
console.log(isRevPer("xyx"));
console.log(isRevPer("xyz"));
console.log(isRevPer("abab"));
console.log(isRevPer("aba"));
Set-Based Approach
In this approach, we use a Set to store all possible substrings and then check if the reverse of each substring is present in the set. This method combines the efficiency of Set lookups with the straightforwardness of substring generation.
Example:
JavaScript
function isRevPerSet(str) {
let subStrSet = new Set();
let n = str.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j <= n; j++) {
subStrSet.add(str.slice(i, j));
}
}
for (let substring of subStrSet) {
let subStringReverse = substring.split('').reverse().join('');
if (!subStrSet.has(subStringReverse)) {
return "No";
}
}
return "Yes";
}
console.log(isRevPerSet("xyx"));
console.log(isRevPerSet("xyz"));
console.log(isRevPerSet("abab"));
console.log(isRevPerSet("aba"));
Frequency-Based Approach
In this approach, we analyze the frequency of each character and their positions in the string. For a string to be perfectly reversible, it should have symmetrical character positions such that any substring’s reverse can be mapped back to a substring in the original string.
Example:
The code example below demonstrates the implementation of the Frequency-Based Approach to check whether a string is perfectly reversible in JavaScript.
JavaScript
function isRevPerFrequency(str) {
let charMap = new Map();
let n = str.length;
for (let i = 0; i < n; i++) {
if (!charMap.has(str[i])) {
charMap.set(str[i], []);
}
charMap.get(str[i]).push(i);
}
for (let [char, positions] of charMap.entries()) {
let len = positions.length;
for (let i = 0; i < Math.floor(len / 2); i++) {
if (positions[i] !== n - 1 - positions[len - 1 - i]) {
return "No";
}
}
}
return "Yes";
}
console.log(isRevPerFrequency("xyx"));
console.log(isRevPerFrequency("xyz"));
console.log(isRevPerFrequency("abab"));
console.log(isRevPerFrequency("aba"));
Similar Reads
JavaScript Reverse a string in place JavaScript reverses a string in place refers to the process of flipping the order of characters within the string without using additional memory. It involves iterating through the string and swapping characters from both ends until reaching the middle. Reverse a string in place ExampleUsing the Jav
5 min read
Reverse a String in JavaScript We have given an input string and the task is to reverse the input string in JavaScript. Reverse a String in JavaScriptUsing split(), reverse() and join() MethodsThe split() method divides the string into an array of characters, reverse() reverses the array, and join() combines the reversed characte
1 min read
JavaScript Strings Coding Practice Problems Strings are one of the most essential data types in JavaScript, used for handling and manipulating text-based data. This curated list of JavaScript string practice problems covers everything master JavaScript Strings. Whether you're a beginner or an experienced developer, these problems will enhance
1 min read
JavaScript String Operators JavaScript String Operators are used to manipulate and perform operations on strings. There are two operators which are used to modify strings in JavaScript. These operators help us to join one string to another string.1. Concatenate OperatorConcatenate Operator in JavaScript combines strings using
3 min read
JavaScript String repeat() Method The repeat() method in JavaScript returns a new string by concatenating the original string a specified number of times. Syntax:string.repeat(count);Parameters:This method accepts a single parameter. count: count is an integer value that shows the number of times to repeat the given string. The rang
3 min read
Javascript Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i"Examples:Â Input: s = "geeks quiz practice code"Â Output: s = "code practice quiz geeks"Input: s = "getting good at coding needs a lot of practice"Â Output: s = "pra
4 min read
JavaScript String Methods JavaScript strings are the sequence of characters. They are treated as Primitive data types. In JavaScript, strings are automatically converted to string objects when using string methods on them. This process is called auto-boxing. The following are methods that we can call on strings.slice() extra
11 min read
String in DSA Using JavaScript A string in JavaScript is a sequence of characters enclosed in single ('), double ("), or backticks (`). Strings in JavaScript are immutable, meaning their contents cannot be changed after creation. Any operation that modifies a string actually creates a new string.Example:JavaScriptlet s = "GfG"; c
2 min read
How are strings stored in JavaScript ? In this article, we will try to understand how strings are stored in JavaScript. Strings are a primitive data type in JavaScript and are allocated a special place in memory to store and manipulate. Unlike some other languages, JavaScript does not have a String Constant Pool. Instead, JavaScript engi
3 min read
JavaScript Array reverse() Method The JavaScript Array reverse() method reverses the order of the elements in an array in place. The first array element becomes the last, and the last element becomes the first, and so on.It modifies the original array and returns a reference to the reversed array. We can also use the Array.toReverse
3 min read