Skip to content

Instantly share code, notes, and snippets.

@praveen-vishnu
Created January 25, 2023 08:46
Show Gist options
  • Save praveen-vishnu/b5bda15d34abafa95812c7a1df3c137d to your computer and use it in GitHub Desktop.
Save praveen-vishnu/b5bda15d34abafa95812c7a1df3c137d to your computer and use it in GitHub Desktop.
Magicaldrome String
/**
*
A Magicaldrome String is a string of characters that reads
the same forward and backward when characters are read in groups of
two and the groups of two are always read from left to right.
For example, "abcdcdab" is a
Magicaldrome String because the first two characters are the same as the
last two characters in the string, namely "ab"; and the 3rd and 4th
characters from the left are the same as the 4th and 3rd characters from
the right, namely "cd".
Keep in mind:
• The characters in the inputString can be rearranged in any way
• There can be multiple Magicaldrome Strings in the same inputString from different rearrangements
• The inputString.length will always be a multiple of 4
• 1 <= inputString.length <= 250
*/
const str = 'abbhhbab';
const str2 = 'abcdcdab';
const str3 = 'ahbhbhah';
const str4 = 'hahbhbha'
const isMagicaldrome = (inputString) => {
if (((inputString.length % 4) == 0) && inputString.length <= 250) {
const first2 = inputString.slice(0, 2);
const last2 = inputString.slice(-2);
if (first2 == last2) {
const right34 = inputString.slice(2, 4);
const left34 = inputString.slice(-4, -2)
if (right34 == left34) {
return 'YES';
}
}
}
return 'NO';
}
console.log(isMagicaldrome(str4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment