Tripadvisor Interview Question

Write a function to remove repeated spaces from a string (ex: "one two three four" -> "one two three four")

Interview Answers

Anonymous

Dec 19, 2014

Just use 2 pointers to walk through and modify the string in place.

Anonymous

Apr 9, 2015

If my understanding is correct. Just "xxx".split("\\s+") and put them back together. Took within 10 lines of code.

Anonymous

Apr 9, 2015

I forgot to mention that do trim() first before split.

Anonymous

Mar 4, 2016

Javascript version: ' This is a test '.trim().replace(/\ {2,}/g, ' ')