In this article, we will see how to check whether the given string is symmetric or not. The symmetrical string is one that is similar from the starting to mid and mid to end.
Example:
Input: khokho Output: The entered string is symmetrical Input: madam Output: The entered string is not symmetrical Input:amaama Output: The entered string is symmetrical Methods to Check Whether the Given String is Symmetric or NotMethod 1: Naive method using loopsExample:
JavaScript
let str = "abcdabc";
let mid = Math.ceil(str.length / 2);
for (let i = 0; i + mid < str.length; i++) {
if (str[i] !== str[mid + i])
return console.log("String is not Symmetric");
}
console.log("The Given string is symmetric");
OutputThe Given string is symmetric
Method 2: Using the slice methodExample:
JavaScript
let str = "abcdabc";
mid = Math.floor(str.length / 2);
if (
str.slice(0, mid) ===
str.slice(mid + (str.length % 2 === 0 ? 0 : 1))
)
console.log("The given string is Symmetric");
else {
console.log("The given string is Not Symmetric");
}
OutputThe given string is Symmetric
Method 3: Using substring method
JavaScript
let str = "abcdabc";
mid = Math.floor(str.length / 2);
if (
str.substring(0, mid) ===
str.substring(mid + (str.length % 2 === 0 ? 0 : 1))
)
console.log("The given string is Symmetric");
else {
console.log("The given string is Not Symmetric");
}
OutputThe given string is Symmetric
Method 4: Using Iterative ApproachExample:
JavaScript
function isSymmetric(str) {
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) {
return false;
}
left++;
right--;
}
return true;
}
let inputStr = "abcdcba";
if (isSymmetric(inputStr)) {
console.log("The given string is Symmetric");
} else {
console.log("The given string is Not Symmetric");
}
OutputThe given string is Symmetric
Method 5: Using split, reverse, and join methodsThis method involves splitting the string into two halves, reversing the second half, and then comparing the two halves.
Example: This method provides a clear and concise way to check for symmetry by leveraging built-in JavaScript methods, making the code easy to understand and maintain.
JavaScript
let str = "abccba";
let mid = Math.floor(str.length / 2);
let firstHalf = str.slice(0, mid);
let secondHalf = str.slice(mid + (str.length % 2 === 0 ? 0 : 1));
// Reverse the second half
secondHalf = secondHalf.split('').reverse().join('');
if (firstHalf === secondHalf) {
console.log("The given string is Symmetric");
} else {
console.log("The given string is Not Symmetric");
}
OutputThe given string is Symmetric
Method 6: Using RecursionIn this approach, we use recursion to check if the string is symmetric. We compare characters from the start and end of the string, moving towards the center. If all corresponding characters are equal, the string is symmetric.
Example: This example demonstrates how to check if a string is symmetric using recursion.
JavaScript
function isSymmetricRecursive(str) {
// Base case: if the string is empty or has one character, it is symmetric
if (str.length <= 1) {
return true;
}
// Check the first and last characters
if (str[0] !== str[str.length - 1]) {
return false;
}
// Recursive call excluding the first and last characters
return isSymmetricRecursive(str.slice(1, -1));
}
// Example usage:
let input1 = "khohkho";
let input2 = "madam";
let input3 = "amaama";
console.log(isSymmetricRecursive(input1));
console.log(isSymmetricRecursive(input2));
console.log(isSymmetricRecursive(input3));
|