Longest Dictionary Word from Nine Letters
Updated
Problem statement
You are given an array of lowercase dictionary words words and a lowercase string letters containing exactly nine characters. Return the longest word that can be built using the characters in letters.
Each occurrence in letters may be used at most once, so repeated characters in a word require the same number of occurrences in letters. If several buildable words have the same maximum length, return the one that appears earliest in words. Return the empty string when no word can be built.
Function
longestWord(words: String[], letters: String) → StringExamples
Example 1
words = ["cat","bt","hat","tree"]letters = "atachxxxx"return = "cat"Both cat and hat can be built and have length three. Because cat appears first, it wins the tie.
Example 2
words = ["hello","world","hold","owl"]letters = "ollheabcd"return = "hello"The letters contain exactly the h, e, two ls, and o needed for hello.
Example 3
words = ["aaa","bb"]letters = "abcdefghi"return = ""Neither word is buildable because the nine letters do not contain enough copies of its repeated character.
Constraints
1 <= words.length <= 100000.1 <= words[i].length <= 100.- The sum of all word lengths is at most
1000000. letters.length == 9.words[i]andletterscontain only lowercase English letters.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
- K-Second Message SuppressionMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Logger Rate LimiterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
- Find Nodes with Invalid Parent LevelsZip · ONSITE INTERVIEWZip interview questions
- Count Word FrequenciesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Most Voted CandidateExpedia · ONSITE INTERVIEWExpedia interview questions
- List Each Name Once, AlphabeticallyDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Shortest Uncommon SubstringAirbnb · ONSITE INTERVIEWAirbnb interview questions