Verify Words Sorted by a Custom Alphabet
Updated
Problem statement
You are given an array of words, words, and an array of letters, alphabet. alphabet lists letters in a scrambled order: a letter that appears earlier in alphabet comes before every letter that appears later.
Compare two words letter by letter from the left. At the first position where they differ, the word whose letter comes earlier in alphabet is smaller. If there is no such position, the shorter word is smaller, and two identical words are equal.
Return true if words is sorted in ascending order under this alphabet, meaning every word is smaller than or equal to the word after it. Otherwise, return false.
Function
isSortedByAlphabet(words: String[], alphabet: char[]) → booleanExamples
Example 1
words = ["cat","bat","tab"]alphabet = ["c","b","a","t"]return = trueThe alphabet order is c, b, a, t.
"cat" and "bat" first differ at their first letter. c comes before b, so "cat" is smaller.
"bat" and "tab" also differ at their first letter. b comes before t, so "bat" is smaller. Every word is smaller than the next one, so the answer is true.
Example 2
words = ["tab","tab","tac"]alphabet = ["t","a","c","b"]return = falseThe first two words are identical, which is allowed.
"tab" and "tac" first differ at their third letter: b and c. In this alphabet, c comes before b, so "tac" is smaller than "tab". A larger word comes before a smaller one, so the answer is false.
Example 3
words = ["ab","a"]alphabet = ["a","b"]return = falseThe words have no differing position: "a" matches the start of "ab". So the shorter word, "a", is smaller.
Here "ab" comes first, so the answer is false.
Constraints
1 <= words.length <= 10001 <= words[i].length <= 1001 <= alphabet.length <= 26alphabetcontains distinct lowercase English letters.- Every letter of every
words[i]appears inalphabet.
More TrueFoundry Phone Screen questions
Similar questions at other companies
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Most Frequent NameAmazon · PHONE SCREEN
- Words with Repeated AnagramsThe Voleon Group · PHONE SCREEN
- Project JSON Fields into a Target FormatBooking.com · PHONE SCREEN
- Product Keyword SearchTesco · PHONE SCREEN
- Character Frequencies Across Nested String ListsSnowflake · PHONE SCREENSnowflake interview questions
- Neighboring Record IDsZipRecruiter · PHONE SCREENZipRecruiter interview questions