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 includedOn 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.
The input is an array of characters, so swap from both ends with two pointers in place.
The lesson's isPalindrome. Clean the string, then compare from both ends.
On the first mismatch, try skipping the left letter or the right one and check the rest as a palindrome.
Split on spaces, drop the empty pieces and join the words back in reverse order.