problem
solution
option 1 - two pointers
1 | class Solution { |
option 2 - dp
1 | class Solution { |
option 3 - *hash table + Binary Search
follow up ,當很多個
s
不斷輸入時
假設 t = "cacbhbc"
,需要一個hash table 存每個字元與方出現的位置,如unordered_map<char, vector<int>>hash_table = {{"a", {1}}, {"b", {3,5}} , {"c", {0,2,6} }, {"h", {4}} }
,當 s="abc"
,且已經匹配 "ab"
了,那這時只需要用二元搜尋查找hash_table["c"]
這個 vector中比4
大的索引。
1 | class Solution { |
analysis
- option 1 - two pointers
- time complexity
O(n)
- space complexity
O(1)
- time complexity
- option 2 - dp
- time complexity
O(nm)
- space complexity
O(nm)
, n = len(s), m= len(t)
- time complexity
- option 3 - hash table + binary search
- time complexity
O(nlogn)
- space complexity
O(1)
- time complexity