Leetcode--Hash Table(python)

3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. 
             Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

找最長的不重複子串長度,可以用字典存儲下每個字符出現的位置,當出現相同字符的時候,就可以得到兩個相同字符之間的長度;
設置一個start的位置,爲了防止"abba"的情況,可以保證每次cur_max更新的時候是從最後一個被比較過的字符的位置後面開始的。

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        if len(s) == 0:
            return 0
        if len(s) == 1:
            return 1
        
        tmp_dict = {}
        start = 0
        res = 0
        cur_max = 0
        for idx, char in enumerate(s):
            if char in tmp_dict and tmp_dict[char] >= start:
                res = max(res, cur_max)
                cur_max = idx - tmp_dict[char]
                start = tmp_dict[char] + 1
            else:
                cur_max += 1
            tmp_dict[char] = idx
                        
        return max(res, cur_max)

30. Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

Example 1:

Input:
  s = "barfoothefoobarman",
  words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:

Input:
  s = "wordgoodgoodgoodbestword",
  words = ["word","good","best","word"]
Output: []

題幹中提到了每個單詞都是等長的,所以可以對s中每lw個字符判斷一下是否在words的字典裏面
判斷concatenation的時候就可以用while循環,兩個break的條件:
第一個是使用tmp字典存儲下每個單詞出現的次數,如果substring中出現的單詞不止一次就退出;
第二個是如果某單詞不存在於字典中就退出。
注意整體上循環的次數應該是ls + 1 - lw*num次,也就是最多能有多少個substring的起始位置

class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        if s=="" or words == []:
            return []
        
        dic = {}
        num = len(words)
        for i in words:
            if i not in dic:
                dic[i] = 1
            else:
                dic[i] += 1
                
        tmp = words[0]
        lw = len(tmp)
        ls = len(s)
        res = []
        
        for i in range(ls + 1 - lw*num):
            tmp = {}
            j=0
            while j < num:
                w = s[i+j*lw:i+(j+1)*lw]
                if w not in dic:
                    break
                
                if w not in tmp:
                    tmp[w] = 1
                else:
                    tmp[w] += 1
                
                if tmp[w] > dic[w]:
                    break
                    
                j+=1
            
            if j == num:
                res.append(i)
                
        return res
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章