
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Joining strings to form palindrome pairs in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of strings as the only argument. The function is supposed to return an array of arrays of all the index pairs joining the strings at which yields a new palindrome string.
For example, if the input to the function is −
const arr = ['tab', 'cat', 'bat'];
Then the output should be −
const output = [[0, 2], [2, 0]];
Output Explanation:
Because both the strings ‘battab’ and ‘tabbat’ are palindromes.
Example
The code for this will be −
const arr = ['tab', 'cat', 'bat']; const isPalindrome = (str = '') => { let i = 0; let j = str.length - 1; while (i < j) { if (str[i] != str[j]) return false; i++; j--; }; return true; }; const palindromePairs = (arr = []) => { const res = []; for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (isPalindrome(arr[i] + arr[j])) { res.push([i, j]) } if (isPalindrome(arr[j] + arr[i])) { res.push([j, i]) }; }; }; return res; }; console.log(palindromePairs(arr));
Code Explanation
We have here used a helper function isPalindome() to check whether a string is palindrome or not and our main function uses all combinations to generate all possible pairs and the ones the match our conditions, their index is pushed in the res array.
Output
And the output in the console will be −
[ [ 0, 2 ], [ 2, 0 ] ]
- Related Articles
- Function to find out palindrome strings JavaScript
- Construct objects from joining two strings JavaScript
- Unique pairs in array that forms palindrome words in JavaScript
- Check if substrings from three given strings can be concatenated to form a palindrome
- Joining two strings with two words at a time - JavaScript
- Construct K Palindrome Strings in C++
- Program to split two strings to make palindrome using Python
- Rearrange characters to form palindrome if possible in C++
- Nearest palindrome in JavaScript
- Palindrome numbers in JavaScript
- Program to check string is palindrome or not with equivalent pairs in Python
- Count all Palindrome Sub-Strings in a String in C++
- Palindrome array - JavaScript
- Joining two Arrays in Javascript
- Python program to count the pairs of reverse strings

Advertisements