Skip to content

Solution #151- Abdul Wahab - 13/07/2025 #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# 151. Reverse Words in a String

**Difficulty:** *Easy*
**Category:** *Strings, Stack, Two Pointers*
**Leetcode Link:** [Problem Link](https://leetcode.com/problems/reverse-words-in-a-string/)

---

## 📝 Introduction

Given a string `s`, reverse the order of words in it. A word is defined as a sequence of non-space characters. The words in `s` are separated by one or more spaces. The output string should only contain a single space separating the words, and no leading or trailing spaces.

---

## 💡 Approach & Key Insights

The main idea is to reverse the order of the words, not the characters within them. We explore multiple approaches starting from brute force using a stack to a space-optimized method that constructs the result while traversing the string from the end.

---

## 🛠️ Breakdown of Approaches

### 1️⃣ Brute Force / Naive Approach

- **Explanation:**
Push each word onto a stack while traversing the string. This reverses the order naturally. Then pop from the stack and build the output string. Stack ensures LIFO behavior to reverse the word order.

- **Time Complexity:** *O(N)* - We traverse the entire string once and then build the result string from the stack.
- **Space Complexity:** *O(N)* - Additional space for the stack and the answer string.

- **Example/Dry Run:**

```plaintext
Input: "TUF is great for interview preparation"

Step 1 → Push each word into a stack:
Stack = ["TUF", "is", "great", "for", "interview", "preparation"]

Step 2 → Pop and build answer:
"preparation interview for great is TUF"

Output: "preparation interview for great is TUF"
```

---

### 2️⃣ Optimized Approach

- **Explanation:**
Traverse the string from the end, constructing each word in reverse. Append each complete word to the result as we detect spaces. This eliminates the need for a stack. We carefully manage whether a space is needed before appending each word.

- **Time Complexity:** *O(N)* - Single traversal of the string.
- **Space Complexity:** *O(1)* - We only use a few extra variables (not counting the output string).

- **Example/Dry Run:**

```plaintext
Input: "TUF is great for interview preparation"

Step 1 → Start from end:
"preparation" → add to result
"interview" → prepend to result
"for" → prepend
"great" → prepend
"is" → prepend
"TUF" → prepend

Final Output: "preparation interview for great is TUF"
```

---

### 3️⃣ Best / Final Optimized Approach (if applicable)

- **Explanation:**
The two-pointer approach traverses from end to start, building words and appending them to the result string efficiently. It avoids stack overhead and minimizes space usage.

- **Time Complexity:** *O(N)*
- **Space Complexity:** *O(1)* (excluding result string)

- **Example/Dry Run:**

```plaintext
Input: " Hello World "

Trim extra spaces
Traverse from the end
Build each word and append
Final Output: "World Hello"
```

---

## 📊 Complexity Analysis

| Approach | Time Complexity | Space Complexity |
| ------------- | --------------- | ---------------- |
| Brute Force | O(N) | O(N) |
| Optimized | O(N) | O(1) |
| Best Approach | O(N) | O(1) |

---

## 📉 Optimization Ideas

- Modify the input string in-place if mutable (e.g., using character arrays in C++).
- Skip multiple spaces early using regex or manual trimming.

---

## 📌 Example Walkthroughs & Dry Runs

```plaintext
Example:
Input: "TUF is great for interview preparation"
Process:
1. Extract words: ["TUF", "is", "great", "for", "interview", "preparation"]
2. Reverse order: ["preparation", "interview", "for", "great", "is", "TUF"]
3. Join with single spaces
Output: "preparation interview for great is TUF"
```

---

## 🔗 Additional Resources


- [C++ String Functions](https://cplusplus.com/reference/string/string/)

---

Author: Abdul Wahab
Date: 19/07/2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
string reverseWords(string s) {
int left = 0;
int right = s.length()-1;

string temp="";
string ans="";

//Iterate the string and keep on adding to form a word
//If empty space is encountered then add the current word to the result
while (left <= right) {
char ch= s[left];
if (ch != ' ') {
temp += ch;
} else if (ch == ' ') {
if (ans!="") ans = temp + " " + ans;
else ans = temp;
temp = "";
}
left++;
}

//If not empty string then add to the result(Last word is added)
if (temp!="") {
if (ans!="") ans = temp + " " + ans;
else ans = temp;
}

return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution:
def reverseWords(self, s: str) -> str:
left = 0
right = len(s) - 1

temp = ""
ans = ""

# Skip leading spaces
while left <= right and s[left] == ' ':
left += 1
# Skip trailing spaces
while right >= left and s[right] == ' ':
right -= 1

# Iterate the string and collect words
while left <= right:
ch = s[left]
if ch != ' ':
temp += ch
else:
if temp != '':
if ans != '':
ans = temp + " " + ans
else:
ans = temp
temp = ""
left += 1

# Add the last word if any
if temp != "":
if ans != "":
ans = temp + " " + ans
else:
ans = temp

return ans