LeetCode - 30. Substring with Concatenation of All Words

时间:2025-04-17 18:36:01

30. Substring with Concatenation of All Words

Problem's Link

----------------------------------------------------------------------------

Mean:

给你一个字符串s,和一个字符串集合words,找出s中words所有单词连续出现的起始下标.

analyse:

思路:窗口滑动算法.

枚举所有长度为words[0].length()的子串,然后使用窗口滑动算法来统计.

Trick:集合words是一个可重集,当连续出现的单词书大于words中的时,需要回退删除开头部分的单词,注意细节.

Time complexity: O(N)

view code

|| ) ; ; ;
           ;
                   left = j + wl;
               }
           }
       }
       return ans;
   }
};