DEV Community

Cover image for Magicaldrome String
Vasile Engineering
Vasile Engineering

Posted on • Updated on

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".

Our task is to take an inputString consisting of lowercase letters and determine whether the string can be rearranged into a Magicaldrome String or not. Ifit can be rearranged into a Magicaldrome String return, "Yes"; if it cannot, return "No".

Example
Input: abbhbhab
Output: Yes

export default function isMagicaldrome(inputString: string): string {
  console.log(inputString);
  if (inputString.length % 2 !== 0) {
    return "no";
  } else {
    let chStr: string = inputString;
    while (chStr.length > 0) {
      if (chStr.slice(0, 2) !== chStr.slice(-2)) {
        return "no";
      }
      chStr = chStr.slice(2, -2);
      console.log(chStr);
    }
    return "yes";
  }
}
console.log(isMagicaldrome("abbhhbab"));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)