Leetcode brush questions: 1657. Determine whether two strings are close, 1004. The maximum number of consecutive 1s III

Leetcode brush questions: 1657. Determine whether two strings are close, 1004. The maximum number of consecutive 1s III

1 Introduction

The above two questions are located in leetcode75 , and the difficulty is medium. Although it may be easy for the big guys, it may still be challenging for me. The final submission efficiency is not very high, I hope you understand.
Please add a picture description

2. 1657. Determine if two strings are close

Label:hash table, string,to sort
The title is as follows:
Two strings are considered close if they can be obtained from one string to another using the following operations:

  • Operation 1: Swap any two existing characters.
    For example, abcde -> aecdb
  • Operation 2: Convert each occurrence of an existing character to another existing character, and do the same for the other character.
    For example, aacabb -> bbcbaa (all a's are converted to b's, and all b's are converted to a's)
    You can use both operations on any string as many times as you want.

You are given two strings, word1 and word2. If word1 and word2 are close, return true; otherwise, return false.
The example is as follows:
Please add a picture description
Algorithm idea (Python): No matter how complex word1 is, if it can be converted into word2 through the above two operations. First, word1 and word2 are used to count the number of characters in the two strings with two hash tables map1 and map2 respectively. First process operation 1, that is, traverse the key in map1, if the key is also in map2, and map1[key]==map2[key], then it means that the number of character keys in word1 and word2 is the same, and it can be proved that it is in word1 Regardless of the position of the character key in the string word1, it can be converted to the corresponding position of the key in word2 through operation 1 (although I don’t know how to prove it), write down these keys, and delete these keys in map1 and map2 after traversing map1 . After these operations, if the two hash tables are all empty, it means that converting word1 to word2 only needs to operate 1 and return True. If the number of keys in the two hash tables is not equal, return False directly; if the keys in the two hash tables do not correspond one-to-one, then return False (that is, if key a is in map1, then key a also needs to be in map2); (operate 2) and then obtain the median value of the two hash tables, use list1 and list2 to store them respectively, sort list1 and list2, and then compare whether the median values ​​of list1 and list2 are the same, as long as one of them is different, Then False, otherwise return True at the end.

The reference code is as follows:

class Solution(object):
    def closeStrings(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: bool
        """

        m = len(word1)
        n = len(word2)
        if m != n:
            return False
        map1 = {
    
    }
        map2 = {
    
    }
        for c in word1:
            map1[c] = map1.get(c,0) + 1
        for c in word2:
            map2[c] = map2.get(c,0) + 1
        arr = []
        for key in map1:
            if not map2.get(key,None):
                return False
            else:
                if map2[key] == map1[key]:
                    arr.append(key)

        for key in arr:
            map1.pop(key)
            map2.pop(key)

        if len(map1.keys()) == 0 and len(map2.keys()) == 0:
            print('第一种情况')
            return True

        print(map1,map2)
        if len(map1.keys()) != len(map2.keys()):
            return False

        count = 0
        n2=  len(map1.keys())
        for key in map1:
            if map2.get(key,None):
                count += 1
        if count != n2:
            return False

        list1 = list(map1.values())
        list2 = list(map2.values())

        list1.sort()
        list2.sort()
        print(list1,list2)
        for i in range(n2):
            if list1[i] != list2[i]:
                return False

        return True



a = Solution()
print(a.closeStrings(word1 = "abbzzca", word2 = "babzzcz"))

operation result:
Please add a picture description

3. 1004. Maximum number of consecutive 1s III

Label:double pointersliding window, Arrays
The topic description is as follows:
Given a binary array nums and an integer k, if at most k 0s can be flipped, then return the maximum number of consecutive 1s in the array.
Examples are as follows:
Please add a picture description

Algorithm ideas: Apply double pointers (left, right) and sliding windows. In this question, a variable count is used to count the number of 0s in the current sliding window. If count is less than or equal to k, then the maximum number of consecutive 1s at this time isright-left+1, if count is greater than k, (if nums[left] == ​​0, count minus 1), left++.
Draw a schematic diagram of the operation of Example 1 as follows:

nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
left=0,right=0,1,count=0,res=1
left=0,right=1,11,count=0,res=2
left=0,right=2,111,count=0,res=3
left=0,right=3,1110,count=1,res=4
left=0,right=4,11100,count=2,res=5
left=1,right=5,11000,count=3,res=5
left=2,right=6,10001,count=3,res=5
left=2,right=6,10001,count=3,res=5
left=3,right=7,00011,count=3,res=5
left=4,right=8,00111,count=2,res=5
left=4,right=9,001111,count=2,res=6
left=4,right=10,0011110,count=3,res=6

The yellow part represents the character string in the sliding serial window.
The reference code is as follows:

class Solution(object):
    def longestOnes(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        n = len(nums)
        left,right=0,0
        count = 0
        res = -1
        while left < n and right < n:
            if count > k:
                if nums[left] == 0:
                    count -= 1
                left += 1

            if nums[right] == 0:
                count += 1

            if count <= k:
                res = max(res,right-left)
            right += 1

        return res + 1

a = Solution()
print(a.longestOnes([1,1,1,0,0,0,1,1,1,1,0],2))

operation result:
Please add a picture description

4. Summary

In general, the topic is fairly simple (if you want to understand, haha!), as for the efficiency is not very high, it is still related to the use of programming language and the time period of submitting code.

Guess you like

Origin blog.csdn.net/qq_45404396/article/details/132115104