DEV Community staff 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.


  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)

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 with your suggestions!

Discussion (2)

citizen428 profile image
Michael Kohl

A simple Nim solution:

import algorithm

func diagonal*(strings: seq[string]): seq[string] =
    len = strings.len
    sorted = strings.sorted
    word: string

  for _ in 0..<len:
    for i in 0..<len:
    word = ""

Didn't feel like writin proper tests, so just stuck this into the same file:

if isMainModule:
  let expected1 = @["1btr", "aae4", "kw3d", "q2ca"]
  assert(diagonal(@["1234", "abcd", "kata", "qwer"]) == expected1)
  assert(diagonal(@["ab", "12"]) == @["1b", "a2"])
_bkeren profile image
'' • Edited on
const solution = (arrayOfStrings) => {
    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("")
    return p;