Skip to content

Latest commit

 

History

History
22 lines (19 loc) · 802 Bytes

reverse-words.md

File metadata and controls

22 lines (19 loc) · 802 Bytes

Reverse Words in a String

Problem

Given an input string, reverse all the words. To clarify, input: “Interviews are awesome!” output: “awesome! are Interviews”. Consider all consecutive non-whitespace characters as individual words. If there are multiple spaces between words reduce them to a single white space. Also remove all leading and trailing whitespaces. So, the output for ” CS degree”, “CS degree”, “CS degree “, or ” CS degree ” are all the same: “degree CS”.

Sample Input 1

s = "Interviews are awesome!"

Python Solution 1

def reverse_words(s):
  words = s.strip().split()
  i = 0
  j = len(words) - 1
  while i <= j:
    words[i], words[j] = words[j], words[i]
    i += 1
    j -= 1
  return " ".join(words)