DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
String

Reverse Words in a String

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Reverse Words in a String** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** Split + reverse + rejoin; handle multiple spaces **Edge Cases to Consider:** - (1) Single word - (2) Multiple leading/trailing spaces - (3) All spaces

Examples

Example 1
Input: "the sky is blue"
Output: "blue is sky the"
Explanation: Classic
Example 2
Input: " hello world "
Output: "world hello"
Explanation: Trim spaces
Example 3
Input: "a good example"
Output: "example good a"
Explanation: Multiple spaces

Constraints

  • ▪1 <= s.length <= 10^4
  • ▪s consists of printable ASCII characters
  • ▪Solve with optimal space complexity

Watch Out For Edge Cases

  • ▪Single word
  • ▪Multiple leading/trailing spaces
  • ▪All spaces
Frequently Asked At
AmazonMicrosoftGoogleAdobeTCS