All topics
124

Characters, codes, and reversing a string

Tier 1Locked

Where it runs

1. The problem

This lesson opens the group about strings. The DP tables of the last lessons, such as edit distance, already worked on strings. Strings also have their own data structure for searching by the start of a word, the trie (a prefix tree); the group gets to it at the end.

A string is a row of characters. A character is one letter, digit, space, or mark. Almost everything you do with text comes down to a walk across that row: reverse it, check it, count it, change it. Two small tasks open the group. The first: reverse a string, so "hello" becomes "olleh". This move comes back later in dozens of forms. The second: decide whether a string is a palindrome, that is, whether it reads the same both ways. And in its practical form: the phrase "A man, a plan, a canal: Panama" must count as a palindrome, even though it has spaces, commas and capital letters. The second task raises two questions that come up in any work with text: how to read a character by its position, and how to clean the input before you compare it.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included

On LeetCode

Easy string problems that reverse text or check a palindrome, often after cleaning the input. Strings cannot change in place in JavaScript, so build a new one or work with two pointers on an array of characters.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy