Strings · String Fundamentals

String Tokenization Reversal

Splitting a string into its meaningful tokens (words, ignoring incidental whitespace) and reassembling them in a different order - separating "what counts as a token" from "what order they end up in."

  • 5 min read
  • 7 practice questions
  • 2 graded problems
On this page
  1. Description
  2. The approach
  3. A second flavor: tokens as keys
  4. Where you'll see it
  5. Which tool for which job?
  6. Common mistakes
  7. Check yourself
  8. Key takeaways
  9. Quick revise
  10. Practice

Description

Reversing the words of a sentence, checking that "dog cat cat dog" follows the pattern "abba", and normalizing a search query all become easy once the text is a list of words. The hard part is getting a clean list of words, and most bugs come from what split actually returns. This skill covers splitting reliably, reassembling with a builder, and using words as keys with a two-way map.

We'll go from simple to more technical:

  1. The pattern: trim, split on runs of whitespace, then reverse and join.
  2. A second flavor: tokens as keys, a one-to-one match with two maps.
  3. Where tokenizing shows up in production (search queries, logs, CSV, commands).
  4. What breaks it: regex separators, empty tokens, vanishing fields, one-way maps.

The approach

Java

String reverseWords(String s) {
    String[] words = s.trim().split("\\s+");    // trim the ends, split on runs of whitespace
    StringBuilder sb = new StringBuilder();
    for (int i = words.length - 1; i >= 0; i--) {
        sb.append(words[i]);
        if (i > 0) sb.append(' ');
    }
    return sb.toString();
}

Python

def reverse_words(s: str) -> str:
    return " ".join(reversed(s.split()))             # split() with no argument: runs of whitespace, no empty strings

Keep "what counts as a word" (the split) separate from "what to do with the words" (reverse and join). The details of split decide the first part:

  • It takes a regex: "1.2.3".split(".") returns an empty array, because . matches every character - write "\\.". The same goes for |, + and *.
  • A leading separator leaves a leading empty string: " hello world ".split("\\s+") is ["", "hello", "world"].
  • Trailing empty strings are dropped: "a,b,,".split(",") has 2 elements; split(",", -1) keeps all 4.
  • split(" ") turns every extra space into an empty token: "a b".split(" ") is ["a", "", "b"].

That's why the pattern trims first and splits on \\s+.

Python's str.split behaves differently. With no argument, s.split() does all of that at once: it splits on runs of whitespace and drops the empty strings at both ends, so reversing words is " ".join(reversed(s.split())). With an argument, s.split(sep) is a different tool: the separator is a literal string, not a regex ("1.2.3".split(".") works), and every empty string is kept, leading and trailing ("a,b,,".split(",") has 4 elements, and "a b".split(" ") is ["a", "", "b"]). re.split is the regex version.

A second flavor: tokens as keys

Once text is tokens, words can be compared and mapped like any other values. "Does s follow pattern?" asks for a one-to-one match between pattern letters and words, and that needs a map in each direction:

Java

boolean wordPattern(String pattern, String s) {
    String[] words = s.split(" ");
    if (words.length != pattern.length()) return false;
    Map<Character, String> toWord = new HashMap<>();
    Map<String, Character> toLetter = new HashMap<>();
    for (int i = 0; i < words.length; i++) {
        char c = pattern.charAt(i);
        if (toWord.containsKey(c) && !toWord.get(c).equals(words[i])) return false;
        if (toLetter.containsKey(words[i]) && toLetter.get(words[i]) != c) return false;
        toWord.put(c, words[i]);
        toLetter.put(words[i], c);
    }
    return true;
}

Python

def word_pattern(pattern: str, s: str) -> bool:
    words = s.split(" ")
    if len(words) != len(pattern):
        return False
    to_word, to_letter = {}, {}
    for c, w in zip(pattern, words):
        if c in to_word and to_word[c] != w:
            return False
        if w in to_letter and to_letter[w] != c:
            return False
        to_word[c] = w
        to_letter[w] = c
    return True

With only the letter-to-word map, "abba" against "dog dog dog dog" passes: a and b both map to "dog" without conflict. The reverse map catches two letters sharing a word. The same two-map check decides whether two strings are isomorphic ("moon"/"deer" yes, "door"/"deed" no).

Where you'll see it

Search boxes trim and collapse whitespace before matching; log processors split lines into fields; command-line tools split arguments. split(",") is not a CSV parser: a quoted field like "Smith, John" contains the separator, and trailing empty fields vanish unless you pass -1 - use a CSV library for real data (Python's csv module).

Which tool for which job?

You needUse
Words separated by any whitespacetrim().split("\\s+") in Java, split() in Python
A literal separator in Javasplit(Pattern.quote(sep))
Keep empty trailing fieldssplit(sep, -1) (Python keeps them already)
Real CSV with quotesa CSV parser, not split(",")
One-to-one pattern matchtwo maps, one in each direction
Reverse words in a mutable bufferreverse everything, then reverse each word

Common mistakes

MistakeWhat happensDo this instead
split(".") in Java0 pieces: "." is a regexquote the separator
split(" ") on uneven spacingempty tokens become stray spacessplit on runs of whitespace
"a,b,,".split(",") in Javatrailing fields vanisha limit of -1
split(",") on quoted CSV"Smith, Jane" splits in twoa CSV parser
trim() / strip() on a non-breaking spaceit stays (Java doesn't count it as whitespace)replace U+00A0, or use (?U)
One map for a pattern matchtwo letters can share a wordtwo maps

Check yourself

Try to answer before opening each one - recalling beats re-reading.

1. What does " hello world ".split("\\s+") return, and why does the pattern call trim() first?

Show answer

["", "hello", "world"] - the leading whitespace leaves a leading empty string, while trailing empty strings are dropped. Without trim(), the empty first token becomes a stray trailing space after reversing.

2. Why does the word-pattern check need a map in each direction?

Show answer

One-to-one means no letter maps to two words and no word is claimed by two letters. The letter-to-word map only catches the first; "abba" vs "dog dog dog dog" slips through until the word-to-letter map sees "dog" claimed by both a and b.

3. Why does "1.2.3".split(".") return an empty array?

Show answer

The argument is a regex, and . matches any character, so every character is a separator and all the pieces are empty - and trailing empty strings are dropped, leaving nothing. Escape it: split("\\.").

Key takeaways

  • Separate tokenizing from processing: split, work on the list, join.
  • split takes a regex, keeps a leading empty string and drops trailing ones (limit -1 keeps them).
  • Use trim() + split("\\s+") for clean words.
  • One-to-one mappings need a map in each direction, plus a length check.
  • In-place word reversal: reverse everything, then each word.

Quick revise

The whole skill on one page, for the night before an interview.

This page puts the lesson and the deep dive together. Skim it before practice or an interview, and go back to the full lesson for anything you don't recognize.

Pick the tool

You needUse
Wordstrim().split("\\s+") / split()
Literal separator (Java)Pattern.quote
Keep trailing fields (Java)limit -1
CSVa CSV parser
Pattern matchtwo maps
In-place reversalreverse all, then each word

Snippets worth knowing by heart

Java

private static final Pattern WS = Pattern.compile("\\s+");      // compile once
String[] words = WS.split(s.replace(' ', ' ').trim());
Collections.reverse(Arrays.asList(words));
String out = String.join(" ", words);

Python

import csv, io
fields = next(csv.reader(io.StringIO(line)))   # handles "Smith, Jane"
words = text.split()                            # runs of whitespace, NBSP included

The traps

  • Regex separators in Java: split(".") returns nothing. Quote the separator.
  • Empty tokens: split(" ") on uneven spacing yields stray empty words. Split on runs of whitespace.
  • Vanishing trailing fields: "a,b,,".split(",") drops the empty ends in Java. Pass a limit of -1.
  • Quoted CSV: "Smith, Jane" splits in two. Use a CSV parser.
  • Non-breaking spaces in Java: trim() leaves U+00A0 in place. Replace it, or use (?U) patterns.
  • One-way maps: two letters can map to the same word. Keep a map in each direction.

Staff-level points, one line each

  • Java: "a.b.c".split(".") gives 0 pieces; "a,b,,".split(",") gives [a, b]; with -1, [a, b, , ].
  • Python keeps trailing fields; split() with no argument splits on runs of whitespace.
  • U+00A0 survives Java's trim() and strip(); only (?U)\s matches it. Python strips it.
  • Naive comma splitting broke "Smith, Jane"; the csv module didn't.
  • A precompiled Pattern was faster than String.split over 300,000 calls.

Say it in an interview

  • "I trim and split on runs of whitespace, so there are no empty tokens."
  • "In Java, split takes a regex, so I quote special separators."
  • "A one-to-one match needs a map in each direction."
  • "For CSV I'd use a parser, not split."

Practice

This skill comes with 7 multiple-choice questions that tell you which misconception a wrong answer reveals and 2 graded problems. Your answers are graded and your mastery is tracked, so you'll know when the skill holds up. Running code and saving progress need a free account.

Practice String Tokenization Reversal free

Word Pattern

EasyFreeCoding problemJava · Python

A pattern of lowercase letters and a sentence s match when you can pair each letter with one word so that reading the pattern letter by letter spells out s: the same letter always stands for the same word, and different letters stand for different words.

Reverse Words in a String

MediumProCoding problemJava · Python

Reverse the order of the words in s. The input may have extra spaces at either end or several between words; the result has exactly one space between words and none at the ends.

The full statement, the examples and the hidden test suite are part of AI Proof Engineers Pro.

Practice String Tokenization Reversal free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime