DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #261 - Diagonal Strings

In this challenge, you'll be given an array of strings with N elements and each element has N length.

First, sort the given array alphabetically. Your output will consist of letters obtained diagonally (from top left to bottom right). Collect the new string, then change the order and obtain a new string. Repeat this process until you have as many strings as was originally given in the array.

Example:

  1234                       Abcd                           
  aBcd                       kAta                           
  kaTa                       qwEr                           
  qweR -> 1234 => "1btr"     1234 -> abcd => "aae4"         

  Kata                       Qwer
  qWer                       1234
  1234                       abCd
  abcD -> kata => "kw3d"     katA -> qwer => "q2ca"

  Output : {"aae4","kw3d","1btr","q2ca"} (by input order)

Tests:
string[] { "1a8er", "B36jh", "AiYe3", "B1t0a", "g47uj" };
string[] { "ab", "12" };

Good luck!


This challenge comes from frkn2076 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (1)

Collapse
 
_bkeren profile image
'' • Edited
const solution = (arrayOfStrings) => {
    arrayOfStrings.sort()
    let resultList = []
    let N = arrayOfStrings[0].length
    for(let i=0; i < N; i++) {
        let diagonalWord = [...Array(N).keys()].map(j => (j+i)%N).map((dIndex,index) => arrayOfStrings[dIndex][index]).join("")
        resultList.push(diagonalWord)
    }
    return p;
}