Leetcode - Strings | Words
58. Length of Last Word[E]
https://leetcode.com/problems/length-of-last-word/
Description
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
If the last word does not exist, return 0.
Note: A word is defined as a maximal substring consisting of non-space characters only.
Example:
1 | Input: "Hello World" |
Solution
1 | class Solution: |
1 | class Solution: |
68. Text Justification[H]
https://leetcode.com/problems/text-justification/
Description
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
Note:
- A word is defined as a character sequence consisting of non-space characters only.
- Each word’s length is guaranteed to be greater than 0 and not exceed maxWidth.
- The input array
wordscontains at least one word.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Example 3:
1 | Input: |
Solution
1 |
79. Word Search[M]
https://leetcode.com/problems/word-search/
Description
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example:
1 | board = |
Constraints:
boardandwordconsists only of lowercase and uppercase English letters.1 <= board.length <= 2001 <= board[i].length <= 2001 <= word.length <= 10^3
Solution
1 |
126. Word Ladder II[H]
https://leetcode.com/problems/word-ladder-ii/
Description
Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
- Only one letter can be changed at a time
- Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
- Return an empty list if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Solution
1 |
127. Word Ladder[M]
https://leetcode.com/problems/word-ladder/
Description
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the word list.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Solution
1 |
290. Word Pattern[E]
https://leetcode.com/problems/word-pattern/
Description
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Example 1:
1 | Input: pattern = "abba", str = "dog cat cat dog" |
Example 2:
1 | Input:pattern = "abba", str = "dog cat cat fish" |
Example 3:
1 | Input: pattern = "aaaa", str = "dog cat cat dog" |
Example 4:
1 | Input: pattern = "abba", str = "dog dog dog dog" |
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.
Solution
1 |
151. Reverse Words in a String[M]
https://leetcode.com/problems/reverse-words-in-a-string/
Description
Given an input string, reverse the string word by word.
Example 1:
1 | Input: "the sky is blue" |
Example 2:
1 | Input: " hello world! " |
Example 3:
1 | Input: "a good example" |
Note:
- A word is defined as a sequence of non-space characters.
- Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
- You need to reduce multiple spaces between two words to a single space in the reversed string.
Follow up:
For C programmers, try to solve it in-place in O(1) extra space.
Solution
1 |
442. Find All Duplicates in an Array[M]
https://leetcode.com/problems/find-all-duplicates-in-an-array/
Description
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
1 | Input: |
Solution
1 |
692. Top K Frequent Words[M]
https://leetcode.com/problems/top-k-frequent-words/
Description
Given a non-empty list of words, return the k most frequent elements.
Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
Example 1:
1 | Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 |
Example 2:
1 | Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 |
Note:
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Input words contain only lowercase letters.
Follow up:
- Try to solve it in O(n log k) time and O(n) extra space.
Solution
1 |
720. Longest Word in Dictionary[E]
https://leetcode.com/problems/longest-word-in-dictionary/
Description
Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
If there is no answer, return the empty string.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Note:
All the strings in the input will only contain lowercase letters.
The length of words will be in the range [1, 1000].
The length of words[i] will be in the range [1, 30].
Solution
1 |
819. Most Common Word[E]
https://leetcode.com/problems/most-common-word/
Description
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn’t banned, and that the answer is unique.
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
Example:
1 | Input: |
Note:
1 <= paragraph.length <= 1000.0 <= banned.length <= 100.1 <= banned[i].length <= 10.- The answer is unique, and written in lowercase (even if its occurrences in
paragraphmay have uppercase symbols, and even if it is a proper noun.) paragraphonly consists of letters, spaces, or the punctuation symbols!?',;.- There are no hyphens or hyphenated words.
- Words only consist of letters, never apostrophes or other punctuation symbols.
Solution
1 |





