We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
lets:String="chi llo ut"varresult:String=""letsWithoutSpaces=s.replacingOccurrences(of:" ",with:"")letstringCount=sWithoutSpaces.countletlenght:Double=Double(stringCount).squareRoot()varnumberOfRows:Int=Int(lenght.rounded(.down))letnumberOfColumns:Int=Int(lenght.rounded(.up))// we can't have a matrix smaller than stringCountifnumberOfColumns*numberOfRows<stringCount{numberOfRows+=1}forcolumnIndexin0..<numberOfColumns{forrowIndexin0..<numberOfRows{letoffset=rowIndex*numberOfColumns+columnIndexguardoffset<stringCountelse{continue}/// This way the index is limited by the endIndex, so we don't bad access the stringlet_index=sWithoutSpaces.index(sWithoutSpaces.startIndex,offsetBy:offset,limitedBy:sWithoutSpaces.endIndex)guardletindex=_indexelse{continue}result+=String(sWithoutSpaces[index])}result+=" "}result.removeLast()result
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Encryption
You are viewing a single comment's thread. Return to all comments →
Swift solution
(note: run it on a playground)