This is the 24th day of my participation in the August Text Challenge.More challenges in August

1859. Put sentences in order

A sentence is a sequence of words connected by a single space, with no Spaces at the beginning and end. Each word contains only lowercase or uppercase English letters. We can add a word position index starting at 1 to a sentence and shuffle all the words in the sentence. For example, the sentence" This is a sentence" can be shuffled to get "sentence4 a3 is2 This1" or "is2 sentence4 This1 A3 ". I give you a scrambled sentence S, which contains no more than 9 words, and ask you to reconstruct and get the original order of the sentence. Example 1: Input: s = "is2 sentence4 This1 A3 "Output: "This is sentence" Explanation: Sort the words in S by their original position to get "This1 is2 A3 sentence4" then delete the number. Example 2: Input: s = "Myself2 Me1 I4 and3" Output: "Me Myself and I" Explanation: Sort the words in S by their original position to get "Me1 Myself2 and3 I4", then delete the numbers. Note: -2 <= s.length <= 200 -s contains only lowercase and uppercase letters, Spaces, and numbers from 1 to 9. -s contains 1 to 9 words. Words in -s are separated by a single space. -s does not contain any leading or postfix Spaces.Copy the code

title

1. Out-of-order sentence strings are separated by Spaces, and letters + numbers are rearranged according to the digits after the words to form a new sentenceCopy the code

Implementation approach

1, split s string into S list 2, no need for loop, 3, iterate to extract elements, split number and word 4, then find the corresponding word according to the index < before this, the number index must be in ascending order >Copy the code

Code implementation

class Solution:
    def sortSentence(self, s: str) - >str:
        split_word=s.split()
        n_word={}
        for i in range(len(split_word)):
            word ,n=split_word[i][:-1].int(split_word[i][-1])
            n_word[n]=word
        s=list(n_word.keys())
        s.sort()
       nword=[]
       for o in s:
           nword.append(n_word[o])
       return "".join(nword)
Copy the code

The execution result

The code analysis

Get the keys of the dictionary and sort them. 4. Traverse the ascending keys and get the corresponding valueCopy the code

Code optimization

From the above code analysis, we know that we can split the element word and number of the list, and then know the index assignment of the list, so that we can know the number -1 at the end of the element to get the index position of the element

Implementation:
class Solution:
    def sortSentence(self, s: str) - >str:
        n_s = s.split()
        n = len(n_s)
        arr = ["" for _ in range(n)]
        # arr = [""] * n # Iterates support the * operation
        for word in n_s:
            arr[int(word[-1]) -1] = word[:-1]
        return "".join(arr)
Copy the code

Execution result: Advantage in execution time

conclusion

2. Algorithms are the constant review of the characteristics of data typesCopy the code