subject
Given a list words containing multiple strings and the order of the user-defined alphabet, return true only when all strings in the list are arranged in the dictionary order in order; Otherwise, false is returned.
For example:
Given a list of words: ["hello", "leetcode"], customize the order of the alphabet: "hlabcdefgijkmnopqrstuvwxyz"
Return result: true
Explanation: in the custom alphabet order, 'h' precedes' l ', so the list is arranged in dictionary order
Given a list of words: ["word", "world", "row"], customize the order of the alphabet: "worldabcefghijkmnpqstuvxyz"
Return result: false
Explanation: in the order of user-defined alphabet,'d 'is after' l ', then words [0] > words [1], so the list is not arranged in dictionary order
explain:
- All characters in words[i] and order are lowercase letters
- White space characters are defined as smaller than other characters. For example, when "apple" and "app" are compared, the first three characters are exactly the same, and then the fourth character of "apple" needs to be compared with the white space character. The final result is that "app" is arranged in front of "apple"
Implementation idea 1
- First, a dictionary is used to store all the letters in order and their order, and then two-level loops are used to judge whether the list is sorted correctly
- The first layer loops through the list, taking the currently traversed string words[i] and its next string words[i+1] each time; The second loop compares the two strings to determine whether the sorting is correct
- In the two strings compared, if the dictionary order of the characters under the subscript j of the current string is greater than that under the subscript j of the next string, it indicates that the list sorting is wrong and returns False directly
- In the two strings to be compared, if the dictionary order of the characters under the subscript j of the current string is equal to the characters under the subscript j of the next string, continue to compare their subscript j+1 characters
- In the two strings compared, if the dictionary order of the characters under the subscript j of the current string is less than the characters under the subscript j of the next string, it indicates that the two strings are sorted correctly and jump out of the current cycle
Code implementation 1
def isDiySorted(words, order): tmp_dict = {c: i for i, c in enumerate(order)} # Order dictionary order for i in range(len(words) - 1): len1, len2 = len(words[i]), len(words[i + 1]) order1, order2 = tmp_dict.get(words[i][0]), tmp_dict.get(words[i + 1][0]) min_word_length = len1 if len1 > len2: # If the next string (such as app) is exactly equal to the first part of the current string (such as apple) if words[i + 1] == words[i][:len2]: return False min_word_length = len2 for j in range(min_word_length): order1, order2 = tmp_dict.get(words[i][j]), tmp_dict.get(words[i + 1][j]) if order1 > order2: # If the character under the subscript j of the current string has a dictionary order greater than the character under the subscript j of the next string return False elif order1 == order2: # If the dictionary order of the characters under the subscript j of the current string is equal to the characters under the subscript j of the next string, continue to compare the j+1 characters continue else: # In other cases, the strings of the current comparison are sorted correctly and directly jump out of the current comparison cycle break return True
Implementation idea 2
- First, use a dictionary to store all the letters in order and their order
- Then use Python's built-in function sorted() to sort the list words
- If the word order of the sorted list is exactly the same as that of the original list, it indicates that the original list is sorted correctly and returns True; otherwise, it returns False
Code implementation 2
def isDiySorted(words, order): """ sorted()Parameters in function key You can pass in a function here key=lambda word: [tmp_dict.get(i) for i in word] sorted()Function handle words Each element in is passed into key , and then returns a list containing the dictionary index of each character in the current element tmp_dict.get(i) ,All numbers When each element returns the corresponding dictionary order subscript list, because its list elements are numbers, sorted()The function automatically sorts them by default """ tmp_dict = {c: i for i, c in enumerate(order)} # Order dictionary order return words == sorted(words, key=lambda word: [tmp_dict.get(i) for i in word])
More Python programming questions are waiting for you to challenge: Python programming problem summary (continuously updated...)