The O(n) Club: Reverse Words in a String III — Don’t Flip Out

The O(n) Club: Reverse Words in a String III — Don’t Flip Out

⚡ TL;DR

Don’t go full Exorcist—just reverse each word in the sentence, not the whole sentence itself. The brute force method? Split by spaces, reverse every word, then join. Java makes this less harrowing than Monday standups:

// Java brute-force using split & StringBuilder
String[] words = s.split(" ");
for (int i = 0; i < words.length; i++) {
    words[i] = new StringBuilder(words[i]).reverse().toString();
}
return String.join(" ", words);

🧠 How Devs Usually Mess This Up

The most common caffeine-induced disaster? Reversing the entire string—goodbye word order, hello gibberish. “Let’s take LeetCode contest” becomes “tsetnoc edoCteeL ekat s’teL”—all very dramatic, none of it correct. Or they get lost fiddling with exotic whitespace handling when the problem literally spoon-feeds you: only one space between words. Simplicity is not a trick question. Don’t invent new formats when you already have a winning lottery ticket.

🔍 What’s Actually Going On

This is not about showing off your regex acrobatics. It’s a split–reverse–join parade: treat each word as a soloist, let them dance backwards, but keep their lineup on stage exactly the same. Imagine a row of robots moonwalking in place—not running around the room.

🛠️ PseudoCode

  • Split input string into words:
    • String[] words = s.split(" ");
    • Because the problem pinky-promised, you can split without fear of lurking whitespace monsters.
  • Reverse each word:
    • for (each word): reverse it with StringBuilder or manual char swap
    • Your choice: flex Java’s StringBuilder or go full “look ma, no library!” interviewer mode.
  • Rejoin with single spaces:
    • String.join(" ", words);
    • Return to sender—just as they like it. Still O(n), still drama-free.

💻 The Code

// Java—fancy version
public String reverseWords(String s) {
    String[] words = s.split(" ");
    for (int i = 0; i < words.length; i++) {
        words[i] = new StringBuilder(words[i]).reverse().toString();
    }
    return String.join(" ", words);
}
 // Manual mode: no split, no join, pure loops (because interviewers love pain)
public String reverseWordsManual(String s) {
    char[] arr = s.toCharArray();
    int start = 0;
    for (int end = 0; end <= arr.length; end++) {
        if (end == arr.length || arr[end] == ' ') {
            reverse(arr, start, end - 1);
            start = end + 1;
        }
    }
    return new String(arr);
}
 private void reverse(char[] arr, int l, int r) {
    while (l < r) {
        char temp = arr[l];
        arr[l++] = arr[r];
        arr[r--] = temp;
    }
}

⚠️ Pitfalls, Traps & Runtime Smacks

  • Don’t reverse everything: Only the words, not the full string—control your urges.
  • Whitespace Assumptions: Problem guarantees simplify your life. If you add complexity, the universe will notice and smite you (or your code).
  • Manual vs Built-in: Know both, because interviewers judge faster than GitHub Actions.
  • Complexity: O(n) time and space—the kind of linear growth accountants and CPUs approve of.

🧠 Interviewer Brain-Tattoo

“Don’t reverse engineer your career—just fix the bits that count.”

Previous Article

The O(n) Club: All Possible Full Binary Trees — Recursion Without Regrets

Next Article

The O(n) Club: Cherry Pickup — How to Outwit a Grid of Thorns, Robots, and Regret