LeetCode OJ: Reverse Words in a String

Following is a simple solution to the LeetCode’s Reverse Words in a String problem.

def reverseWords(s: String) = 
  s.split (" ")
   .reverse
   .mkString(" ")

Lets execute it in the REPL.

scala> reverseWords("the sky is blue")
res14: String = blue is sky the
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: