Strings for Introduction to Algorithms (Python) [Elementary Algorithms - Strings] [Lanqiao Cup Exercises] [Likou Exercises]


1. Reverse the string (simple)

topic:

Write a function that reverses the input string. The input string is given as a character array s.
Don't allocate extra space for another array, you have to modify the input array in-place, using O(1) extra space to solve this problem.

Example 1:

Input: s = ["h", "e", "l", "l", "o"]
Output: ["o", "l", "l", "e", "h"]

Example 2:

Import: s = ["H","a","n","n","a","h"] Export: ["
h","a","n","n","a ”,“H”]

Source: LeetCode
Link: https://leetcode-cn.com/problems/reverse-string

analyze:

Swap the beginning and the end directly.

code:

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        n = len(s)
        j = n - 1
        for i in range(n//2):
            x = s[i]
            s[i] = s[j]
            s[j] = x
            j -= 1

2. Integer inversion (medium)

topic:

Given a 32-bit signed integer x, return the result of inverting the numeric portion of x.
Returns 0 if the inverted integer exceeds the range [−231, 231 − 1] of a 32-bit signed integer.
Assume the environment does not allow storing 64-bit integers (signed or unsigned).

Example 1:

Input: x = 123
Output: 321

Example 2:

Input: x = -123
Output: -321

Example 3:

Input: x = 120
Output: 21

Example 4:

Input: x = 0
Output: 0

Source: LeetCode
Link: https://leetcode-cn.com/problems/reverse-integer

analyze:

After taking the absolute value of the number, convert it into a string, then directly use the slice operation to reverse the order of the string, and then convert it into an integer, judge the sign of the initial value, determine the sign of the final return value, and finally judge whether it is in Return the value after it is within the range.

code:

class Solution:
    def reverse(self, x: int) -> int:
        y = abs(x) #取绝对值
        y = str(y) #转为字符串
        z = y[::-1] #倒序
        z = int(z) #转为整型
        if x < 0: #判断正负
            z = -z
        if z < (-2) ** 31 or z > 2**31 - 1: #判断是否在取值范围内
            return 0
        return z

3. The first unique character in a string (simple)

topic:

Given a string, find its first unique character and return its index. Returns -1 if not present.

Example:

s = "leetcode"
returns 0

s = "loveleetcode"
returns 2

hint:

You can assume that the string contains only lowercase letters.

Source: LeetCode
Link: https://leetcode-cn.com/problems/first-unique-character-in-a-string

analyze:

I used it two ways.

  1. Traverse the string to count and store the number of occurrences of each character, and finally traverse once to return the character that appears only once for the first time, and return its subscript.
  2. Because there are only lowercase letters, we can define a 26 * 2 array to represent a – z, row subscript 0: a, row subscript 1: b, and so on. The column subscript 0 indicates the number of occurrences of each letter, and the subscript 0 is used to store the position where the letter appears in the string. Finally, the two-dimensional array is traversed to count the subscripts with a count of 1, and then sorted to get the smallest subscript, which is the unique character that appears for the first time.

code:

The first:

class Solution:
    def firstUniqChar(self, s: str) -> int:
        count = collections.Counter(s)
        for i, ch in enumerate(s):
            if count[ch] == 1:
                return i
        return -1

The second type:

class Solution:
    def firstUniqChar(self, s: str) -> int:
        n = [[0] for i in range(26)] #二维数组
        count = []  #存储只出现了一次的字符的下标
        for i in range(len(s)):
            x = ord(s[i]) - ord('a')  #用ascll码来判断字符
            n[x][0] += 1 #出现次数统计
            n[x].append(i) #添加下标
        for i in range(26):  #找出只出现一次的字符
            if n[i][0] == 1:
                 count.append(n[i][1])
        count.sort() #下标排序
        if len(count) == 0:
            return -1
        return count[0]    #输出第一个                


4. Effective anagrams (simple)

topic:

Given two strings s and t, write a function to determine whether t is an anagram of s.
Note: If the number of occurrences of each character in s and t is the same, then s and t are said to be anagrams of each other.

Example 1:

Input: s = “anagram”, t = “nagaram”
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

hint:

1 <= s.length, t.length <= 5 * 104
s and t contain only lowercase letters

analyze:

Python can directly use functions to solve. But it is not recommended, because the truth is a bit shameless hhh. I am directly using to sort the strings, then traverse the strings, if there is a difference, it is not an anagram, if not, it is.

Source: LeetCode
Link: https://leetcode-cn.com/problems/valid-anagram

code:

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        return collections.Counter(s)==collections.Counter(t)
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        s = list(s)
        t = list(t)
        n = len(s)
        m = len(t)
        if n != m:
            return False
        s.sort()
        t.sort()
        for i in range(n):
            if s[i] != t[i]:
                return False
        return True

# Five, verify the palindrome string (simple) **Title:**

Given a string, verify that it is a palindrome, considering only alphanumeric characters, ignoring the case of letters.
Explanation: In this question, we define an empty string as a valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome

Example 2:

Input: "race a car"
Output: false
Explanation: "raceacar" is not a palindrome

hint:

1 <= s.length <= 2 * 105
String s consists of ASCII characters

Source: LeetCode
Link: https://leetcode-cn.com/problems/valid-palindrome

analyze:

Regardless of the case of the letter, we can unify it to lowercase, and then point to the rest of the characters in the string, we can use the ascll code to filter. Then double pointer to get the result.

code:

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = s.lower()
        ss = []
        a = int(ord('a'))
        z = int(ord('z'))
        asc0 = int(ord('0'))
        asc9 = int(ord('9'))
        n = len(s)
        for i in range(n):
            x = int(ord(s[i]))
            letter = x >= a and x <= z
            num = x >= asc0 and x <= asc9
            if letter or num:
                ss.append(s[i])
        m = len(ss)
        if m == 0:
            return True
        j = m - 1
        for i in range(m//2):
            if ss[i] != ss[j]:
                return False
            j -= 1
        return True

6. Convert string to integer (atoi) (medium)

I have written a separate article on this question, see:

https://blog.csdn.net/youngwyj/article/details/122662959


7. Implement strStr() (simple)

Topic:
Implement the strStr() function.
Given you two strings haystack and needle, please find the first position of the string needle in haystack string (subscript starts from 0). Returns -1 if not present.

illustrate:

What should we return when needle is an empty string? This is a great question to ask in an interview.
For this question, we should return 0 when needle is an empty string. This matches the definition of strstr() in C and indexOf() in Java.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Example 3:

Input: haystack = "", needle = ""
Output: 0

hint:

0 <= haystack.length, needle.length <= 5 * 104
haystack and needle consist of lowercase English characters only

Source: LeetCode
Link: https://leetcode-cn.com/problems/implement-strstr

analyze:

First judge whether the length of the two strings is legal, that is, the length of haystack is longer than or equal to that of neddle. Then start traversing from the first character of haystack, if the i-th character of haystack is the same as the first character of neddle, judge whether the characters behind haystack can form a neddle, and return the subscript if so.

code:

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        n = len(haystack) #h长度
        m = len(needle) #n长度
        if m == 0:
            return 0
        if n < m:
            return -1
        
        for i in range(n - m + 1): #遍历h,遍历的长度为h的长度减去n的长度加一,因为长度不够一定组不成n 
            if haystack[i] == needle[0]: #h的第i个字符是否与n的第一个字符相同
                if haystack[i:i + m] == needle: #h后面的m个字符是否与n相同
                    return i
        return -1

8. Appearance sequence (medium)

topic:

Given a positive integer n, output the nth item of appearance sequence.
The "appearance sequence" is a sequence of integers, starting from the number 1, and each item in the sequence is a description of the previous item.
You can think of it as a sequence of numeric strings defined by a recursive formula:

countAndSay(1) = "1"
countAndSay(n) is a description of countAndSay(n-1), then converted into another string of numbers.

The first five are as follows:

  1. 1
    
  2. 11
    
  3. 21
    
  4. 1211
    
  5. 111221
    

The first item is the number 1
to describe the previous item. This number is 1, which means "one 1", which is recorded as "11"
to describe the previous item. This number is 11, which means "two 1s", which is recorded as "21"
to describe the previous item. Item, this number is 21, namely "one 2 + one 1", recorded as "1211"
to describe the previous item, this number is 1211, namely "one 1 + one 2 + two 1", recorded as "111221"
to describe a For numeric strings, first split the string into a minimum number of groups, each group consisting of at most consecutive identical characters. Then for each group, first describe the number of characters, and then describe the characters, forming a description group. To convert a description to a numeric string, replace the number of characters in each group with a number, then concatenate all description groups.

For example, the description of the numeric string "3322251" is as follows:

Example 1:

Input: n = 1
Output: "1"
Explanation: This is a basic example.

Example 2:

Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = read "1" = one 1 = "11"
countAndSay(3) = read "11" = two 1s = "21"
countAndSay(4) = read "21" = a 2 + a 1 = "12" + "11" = "1211"

hint:

1 <= n <= 30

Source: LeetCode
Link: https://leetcode-cn.com/problems/count-and-say

analyze:

This question is to count the number of adjacent identical numbers in the previous string. It can be generated directly through traversal.

code:

class Solution:
    def countAndSay(self, n: int) -> str:
        s1 = ['1']
        count = 0
        for i in range(n):
            m = len(s1[i])
            num = []
            for j in range(m):
                if j != m - 1:
                    if s1[i][j] == s1[i][j + 1]:
                        count += 1
                    else:
                        count += 1
                        num.append(str(count))
                        num.append(s1[i][j])
                        count = 0
                else:
                    count += 1
                    num.append(str(count))
                    num.append(s1[i][j])
                    count = 0
            a = ''.join(num)
            s1.append(a)
        return s1[n - 1]

Of course, there is a real violent solution:

class Solution:
    def countAndSay(self, n: int) -> str:
        num = {
    
    
            1:"1",
            2:"11",
            3:"21",
            4:"1211",
            5:"111221",
            6:"312211",
            7:"13112221",
            8:"1113213211",
            9:"31131211131221",
            10:"13211311123113112211",
            11:"11131221133112132113212221",
            12:"3113112221232112111312211312113211",
            13:"1321132132111213122112311311222113111221131221",
            14:"11131221131211131231121113112221121321132132211331222113112211",
            15:"311311222113111231131112132112311321322112111312211312111322212311322113212221",
            16:"132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211",
            17:"11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221",
            18:"31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211",
            19:"1321132132211331121321231231121113112221121321132122311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112111331121113122112132113213211121332212311322113212221",
            20:"11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211",
            21:"311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311122122111312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221131112311311121321122112132231121113122113322113111221131221",
            22:"132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113213221133122112231131122211211131221131112311332211211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211",
            23:"111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121113222123112221221321132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221",
            24:"3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211",
            25:"132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132132211231232112311321322112311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312111312212231131122211311123113322112111312211312111322111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113213221132213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121132211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221",
            26:"1113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123211211131211121311121321123113111231131122112213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122113221122112133221121113122113121113222123211211131211121311121321123113213221121113122113121113222113221113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211",
            27:"31131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321322123211211131211121332211231131122211311122122111312211213211312111322211231131122211311123113322112111331121113112221121113122113111231133221121113122113121113222123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221123113112221131112311332111213122112311311123112111331121113122112132113311213211321222122111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112132113213221133112132123123112111311222112132113311213211231232112311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122211311123113322113223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331221122311311222112111312211311123113322112132113213221133122211332111213112221133211322112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122211331121321232221121113122113121122132112311321322112111312211312111322211213111213122112132113121113222112132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212321121113121112133221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213212312311211131122211213211331121321122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311222113111221221113122112132113121113222112132113213221133122211332111213322112132113213221132231131122211311123113322112111312211312111322212321121113122123211231131122113221123113221113122112132113213211121332212311322113212221",
            28:"13211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221232112111312211312113211223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321322113311213212322211322132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212311222122132113213221123113112221133112132123222112111312211312111322212311322123123112111321322123122113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132132211331221122311311222112111312211311123113322112111312211312111322212311322123123112112322211211131221131211132221132213211321322113311213212322211231131122211311123113321112131221123113112211121312211213211321222113222112132113223113112221121113122113121113123112112322111213211322211312113211",
            29:"11131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211132221121311121312211213211312111322211213211321322113311213212322211231131122211311123113223112111311222112132113311213211221121332211211131221131211132221231122212213211321322112311311222113311213212322211211131221131211132221232112111312111213322112131112131221121321131211132221121321132132212321121113121112133221121321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122112131112131221121321132132211231131122111213122112311311222113111221131221221321132132211331121321231231121113112221121321133112132112211213322112311311222113111231133211121312211231131122211322311311222112111312211311123113322112132113212231121113112221121321132122211322212221121123222112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311222112111331121113112221121113122113121113222112132113213221232112111312111213322112311311222113111221221113122112132113121113222112311311222113111221132221231221132221222112112322211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212321121113121112133221132211131221131211132221232112111312111213322112132113213221133112132113221321123113213221121113122123211211131221222112112322211231131122211311123113321112132132112211131221131211132221121321132132212321121113121112133221123113112221131112311332111213211322111213111213211231131211132211121311222113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331121321232221123123211231132132211231131122211331121321232221123113112221131112311332111213122112311311123112112322211211131221131211132221232112111312211322111312211213211312111322211231131122111213122112311311221132211221121332211213211321322113311213212312311211131211131221223113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112211213322112312321123113213221123113112221133112132123222112311311222113111231132231121113112221121321133112132112211213322112311311222113111231133211121312211231131112311211133112111312211213211312111322211231131122111213122112311311221132211221121332211211131221131211132221232112111312111213111213211231132132211211131221232112111312211213111213122112132113213221123113112221133112132123222112111312211312111322212311222122132113213221123113112221133112132123222112311311222113111231133211121321132211121311121321122112133221123113112221131112311332211322111312211312111322212321121113121112133221121321132132211331121321231231121113112221121321132122311211131122211211131221131211322113322112111312211322132113213221123113112221131112311311121321122112132231121113122113322113111221131221",
            30:"3113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112212211131221121321131211132221123113112221131112311332211211133112111311222112111312211311123113322112111312211312111322212321121113121112133221121321132132211331121321132213211231132132211211131221232112111312212221121123222112311311222113111231133211121321321122111312211312111322211213211321322123211211131211121332211231131122211311123113321112131221123113111231121123222112111331121113112221121113122113111231133221121113122113121113221112131221123113111231121123222112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211231131122211311123113321112131221123113111231121113311211131221121321131211132221123113112211121312211231131122211211133112111311222112111312211312111322211213211321223112111311222112132113213221133122211311221122111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321132132211322132113213221123113112221133112132123222112111312211312112213211231132132211211131221131211322113321132211221121332211213211321322113311213212312311211131122211213211331121321123123211231131122211211131221131112311332211213211321223112111311222112132113213221123123211231132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113312211223113112221121113122113111231133221121321132132211331222113321112131122211332113221122112133221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112112322211322311311222113111231133211121312211231131112311211232221121113122113121113222123211211131221132211131221121321131211132221123113112211121312211231131122113221122112133221121321132132211331121321231231121113121113122122311311222113111231133221121113122113121113221112131221123113111231121123222112132113213221133112132123123112111312211322311211133112111312211213211311123113223112111321322123122113222122211211232221121113122113121113222123211211131211121311121321123113213221121113122123211211131221121311121312211213211321322112311311222113311213212322211211131221131211221321123113213221121113122113121113222112131112131221121321131211132221121321132132211331121321232221123113112221131112311322311211131122211213211331121321122112133221121113122113121113222123112221221321132132211231131122211331121321232221121113122113121113222123211211131211121332211213111213122112132113121113222112132113213221232112111312111213322112132113213221133112132123123112111311222112132113311213211221121332211231131122211311123113321112131221123113112221132231131122211211131221131112311332211213211321223112111311222112132113212221132221222112112322211211131221131211132221232112111312111213111213211231131112311311221122132113213221133112132123222112311311222113111231132231121113112221121321133112132112211213322112111312211312111322212321121113121112131112132112311321322112111312212321121113122122211211232221121311121312211213211312111322211213211321322123211211131211121332211213211321322113311213211322132112311321322112111312212321121113122122211211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113111231133221121321132122311211131122211213211321222113222122211211232221123113112221131112311332111213122112311311123112111331121113122112132113121113222112311311221112131221123113112221121113311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213213211221113122113121113222112132113213221232112111312111213322112132113213221133112132123123112111312211322311211133112111312212221121123222112132113213221133112132123222113223113112221131112311332111213122112311311123112112322211211131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112211322212322211231131122211322111312211312111322211213211321322113311213211331121113122122211211132213211231131122212322211331222113112211"
        }
        return num.get(n, None)

Nine, the longest common prefix (simple)

topic:

Write a function to find the longest common prefix in an array of strings.
Returns the empty string "" if no common prefix exists.

Example 1:

Input: strs = ["flower", "flow", "flight"]
Output: "fl"

Example 2:

Input: strs = ["dog", "racecar", "car"]
Output: ""
Explanation: No common prefix exists for the inputs.

hint:

1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of lowercase English letters only

Source: LeetCode
Link: https://leetcode-cn.com/problems/longest-common-prefix

analyze:

For this question, I directly use the first string as the benchmark to judge whether the first i characters of the subsequent strings are the same, and end if there is a difference. It is reasonable to find the shortest one, I am not trying here.

code:

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        for i in range(1,len(strs[0]) + 1):  #字符串的前 i 个字符
            for j in range(1,len(strs)):  #遍历字符串
                if strs[0][:i] == strs[j][:i]:
                    continue
                else:
                    return strs[0][:i - 1]
            if i == len(strs[0]):
                return strs[0]
        return ""

Code words are not easy, please leave a like before leaving.
Follow me, learn together, and make progress together.

Guess you like

Origin blog.csdn.net/youngwyj/article/details/122745545