leetcode 30. Concatenate substrings of all words (high-quality solution)

Code:

class Solution {
    public static List<Integer> findSubstring(String s, String[] words) {
        List<Integer> integerList=new ArrayList<>();

        int length=words.length;    //words 数组中的字符串个数
        int size=words[0].length(); //words 数组中每个字符串的长度

        HashMap<String,Integer> hashMap1=new HashMap<>();   //存储 words 数组中字符串以及出现的个数


        //把 words 数组中字符串以及出现的个数保存到 hashMap1 中
        for(String str:words){
            hashMap1.put(str,hashMap1.getOrDefault(str,0)+1);
        }

        for(int i=0;i<size;i++){
            int count=0;    //记录当前子串中符合条件的字符串
            HashMap<String,Integer> hashMap2=new HashMap<>();   //存储讨论的子串中字符串以及出现的个数
            for(int left=i,right=i;right+size<=s.length();right+=size){
                //把 right 指针指向的数据入窗口
                String in=s.substring(right,right+size);
                hashMap2.put(in,hashMap2.getOrDefault(in,0)+1);

                //判断当前入窗口的字符串是否是符合条件的
                if(hashMap2.get(in)<=hashMap1.getOrDefault(in,0)){
                    count++;
                }

                //判断当前子串的长度是否过长,是否需要出窗口
                if(right-left+1>length*size){
                    String out=s.substring(left,left+size);
                    //判断出窗口的字符串是否是有效字符串
                    if(hashMap2.get(out)<=hashMap1.getOrDefault(out,0)){
                        count--;
                    }
                    hashMap2.put(out,hashMap2.get(out)-1);
                    left+=size;
                }

                //判断有效字符串个数是否符合条件
                if(count==length){
                    integerList.add(left);
                }
            }
        }
        return integerList;
    }
}

answer:

        The meaning of this question is very clear. We need to find a substring in the string s. The substring consists of the strings in the string array words in different orders.

        We can first think of a brute force solution, traverse all the substrings in the string s, and find substrings that are completely composed of the strings in words in different orders. Now it involves a question, how do we know that the substring is completely composed of the strings in words in different orders?

        We can use the hash tables hash1 and hash2 to store information about the strings in the words array and the substring, using the string as the key and the number as the value, and then compare the contents in hash1 and hash2 to know whether the current substring is eligible

        SupposeInput:s = "barfoothefoobar", words = ["foo","bar"]

        First of all, the information we can get is that the number of strings in the words array is length = 2, and the size of each string is size = 3. Since we want to traverse all substrings, let the L and R pointers Points to position at index 0. Wesave the relevant information in the words array into the hash table hash1, foo-1, bar-1, Use the variable count to record the number of valid strings in the substring

          Because the substrings that meet the requirements are composed of strings in the words array in different orders, and the strings in the words array are 3 characters as a whole, so when we are looking for the substring, we also use 3 characters are a whole, the R pointer points to the current position, take out the string bar through s.substring(right,right+size), and save bar - 1 to hash2. Since bar-1 is in hash1, As long as the number of characters in the substring is less than or equal to the number of corresponding characters in the wors array, it means that the character is a valid character, so bar is a valid character. At this time, count++ , count=1

b        a        r        f        o        o        t        h        e        f        o        o        b        a        r      

L

R

     After entering the information of the string bar, the R pointer moves size bits to the right, enters the next string foo, and saves foo-1 to hash2. Since foo-1 is in hash1, foo is a valid character. At this time, count++ , count=2, because at this time the substring is barfoo, the length is 6 == size*length, and count == 2 == length, so the substring meets the requirements, we will directly record the subscript 0 pointed to by the L pointer

b        a        r        f        o        o        t        h        e        f        o        o        b        a        r      

L

                             R  

        When the R pointer is moved to the right by size bits, the substring is barfoothe, with a size of 9 > size * length. The number of characters is more than the number of characters in the words array, which definitely does not meet the requirements. It represents the substring headed by the L pointer. The string has been discussed. Let the L pointer move size bits to the right and discuss the next set of strings.

b        a        r        f        o        o        t        h        e        f        o        o        b        a        r           

L

                                                           R        

        The L pointer previously pointed to the string bar. The number of bar in hash2 is 1, which is less than or equal to the number in hash1, so it is a valid string. Therefore, before moving the L pointer, we need to modify bar in hash2. - 0, count = 1,

At this time, the length of the substring meets the requirements, but the number of valid strings count < length, so the substring does not meet the requirements, so move the R pointer to the right by size bits 

b        a        r        f        o        o        t        h        e        f        o        o        b        a        r           

                              L  

                                                           R 

        After that, the above operation can be circulated

        Careful friends will find that the above process has not discussed all the substrings. We still need to continue the above operation in the following two situations, that is, the above loop operation must be executed size times

b        a        r        f        o        o        t        h        e        f        o        o        b        a        r           

           L

           R

    

b        a        r        f        o        o        t        h        e        f        o        o        b        a        r           

                    L

                    R

      

おすすめ

転載: blog.csdn.net/q322359/article/details/134944265