Scala solution

• ``````object Solution {
def fullJustify(words: Array[String], maxWidth: Int): List[String] = helper(0, words, maxWidth)

private def helper(p:Int, words: Array[String], maxWidth: Int): List[String] =
if(p==words.size)
List()
else{
var (str, spaces) = nextLine(p, words, maxWidth)
val blanks = str.size / 2
(
if(str.size==1)
List(str(0)," " * spaces)
else if(( str.size + 1 ) / 2 + p == words.size)
str :+ (" " * (spaces - blanks ))
else{
var f = 1
str.map(x => {
f = 1 - f
if(f==0)
x
else if( spaces % blanks == 0 )
" " * (spaces / blanks)
else{
spaces -= 1
" " * ( (spaces + 1) / blanks + 1)
}
})
}
).mkString::helper(p + ( str.size + 1 ) / 2, words, maxWidth)
}

private def nextLine(p:Int, words: Array[String], maxWidth: Int): (List[String], Int) =
if(p==words.size || words(p).length>maxWidth)
(List(), maxWidth)
else {
val (rest, spaces) = nextLine(p+1, words, maxWidth - words(p).length - 1)
if(rest.size==0)
(words(p)::rest, spaces+1)
else
(words(p)::" "::rest, spaces+1)
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.