当前位置:网站首页>Leetcode (524) -- match the longest word in the dictionary by deleting letters
Leetcode (524) -- match the longest word in the dictionary by deleting letters
2022-07-01 02:01:00 【SmileGuy17】
Leetcode(524)—— Delete the longest letters from the dictionary
subject
Give you a string s And an array of strings dictionary , Find out and return to dictionary The longest string in , The string can be deleted s Some characters in get .
If there is more than one answer , Returns the string with the longest length and the smallest alphabetical order . If the answer doesn't exist , Returns an empty string .
Example 1:
Input :s = “abpcplea”, dictionary = [“ale”,“apple”,“monkey”,“plea”]
Output :“apple”
Example 2:
Input :s = “abpcplea”, dictionary = [“a”,“b”,“c”]
Output :“a”
Tips :
- 1 1 1 <= s.length <= 1000 1000 1000
- 1 1 1 <= dictionary.length <= 1000 1000 1000
- 1 1 1 <= dictionary[i].length <= 1000 1000 1000
- s and dictionary[i] It's only made up of lowercase letters
Answer key
Method 1 : greedy + Double pointer
Ideas
According to the meaning , We need to solve two problems :
- How to determine dictionary \textit{dictionary} dictionary String in t t t Can I delete s s s Some characters in get ;
- How to find the string with the longest length and the smallest dictionary order .
The first 1 1 1 The first problem is actually judgment t t t Whether it is s s s The subsequence . So as long as you can find any one t t t stay s s s The way they appear in , It can be considered that t t t yes s s s The subsequence . And when we match from front to back , You can find that every time Greedily match the top character ( Until I find s s s The first place in d i c t i o n a r y [ x ] [ j ] dictionary[x][j] dictionary[x][j] The right position ) Is the optimal decision .
Assume that you currently need to match characters c c c, And characters c c c stay s s s Position in x 1 x_1 x1 and x 2 x_2 x2 appear ( x 1 < x 2 x_1 < x_2 x1<x2), So greedy x 1 x_1 x1 It is the optimal solution. , because x 2 x_2 x2 Characters that can be accessed later , x 1 x_1 x1 Can also get , And through x 1 x_1 x1 And x 2 x_2 x2 Optional characters between , There is more hope that the match will succeed .
such , We initialize two pointers i i i and j j j, Point to respectively t t t and s s s Initial position . Match greedily every time , If the match is successful i i i and j j j Move right at the same time , matching t t t Next position of , If the match fails, then j j j Move right , i i i unchanged , Try to use s s s Match the next character of t t t .
In the end, if i i i Move to t t t At the end of , shows t t t yes s s s The subsequence .
The first 2 2 2 Questions can be traversed dictionary \textit{dictionary} dictionary String in , And maintain the string with the longest current length and the smallest dictionary order to find .
Code implementation
My own :
class Solution {
public:
string findLongestWord(string s, vector<string>& dictionary) {
// Traverse dictionary String in , Then compare them one by one s , And judge whether it can be deleted s Some characters in get
// maxL The string with the longest length and the smallest alphabetical order is in dictionary Subscript in
int maxL = -1, ptr, size_d = dictionary.size(), size_s = s.size();
for(int n = 0; n < size_d; n++){
// First compare the current string with maxL Represents the length of the string
// If the current string length is shorter than maxL Or both are equal in length but the alphabetic order is greater than or equal to maxL Then find the next string
if(maxL != -1 && (dictionary[n].size() < dictionary[maxL].size() ||
(dictionary[n].size() == dictionary[maxL].size() && dictionary[n] >= dictionary[maxL]))) continue;
ptr = 0; // Point to the current string
for(int i = 0; i < size_s && ptr < dictionary[n].size(); i++)
if(s[i] == dictionary[n][ptr]) ptr++;
if(ptr == dictionary[n].size()) maxL = n;
}
return maxL == -1? "": dictionary[maxL];
}
};
Complexity analysis
Time complexity : O ( d × ( m + n ) ) O(d×(m+n)) O(d×(m+n)), among d d d Express dictionary \textit{dictionary} dictionary The length of , m m m Representation string s s s The length of , n n n Express dictionary \textit{dictionary} dictionary Average length of string in . We need to traverse dictionary \textit{dictionary} dictionary Medium d d d A string , Each string requires O ( n + m ) O(n+m) O(n+m) To determine whether the string is s s s The subsequence .
Spatial complexity : O ( 1 ) O(1) O(1)
Method 2 : Double pointer + Sort preprocessing
Ideas
On the basis of method one , We try to pass on dictionary \textit{dictionary} dictionary Preprocessing , To optimize the 2 2 2 How to deal with these problems .
We can first dictionary \textit{dictionary} dictionary Sort according to the descending order of string length and the ascending order of dictionary order , Then find the first qualified string from the front to the back and return it directly .
Code implementation
My own :
class Solution {
public:
string findLongestWord(string s, vector<string>& dictionary) {
// Sort
sort(dictionary.begin(), dictionary.end(),
[](string& A, string& B){
return A.size() > B.size() || (A.size() == B.size() && A < B);});
int ptr, size_d = dictionary.size(), size_s = s.size();
for(int n = 0; n < size_d; n++){
ptr = 0; // Point to the current string
for(int i = 0; i < size_s && ptr < dictionary[n].size(); i++)
if(s[i] == dictionary[n][ptr]) ptr++;
if(ptr == dictionary[n].size()) return dictionary[n];
}
return "";
}
};
Complexity analysis
Time complexity : O ( d × m × l o g d + d × ( m + n ) ) O(d×m×logd+d×(m+n)) O(d×m×logd+d×(m+n)), among d d d Express dictionary \textit{dictionary} dictionary The length of , m m m Express s s s The length of , n n n Express dictionary \textit{dictionary} dictionary Average length of string in . We need to O ( d × m × log d ) O(d \times m \times \log d) O(d×m×logd) Time to sort dictionary \textit{dictionary} dictionary; In the worst case , We need to O ( d × ( m + n ) ) O(d \times (m+n)) O(d×(m+n)) To find the first qualified string .
Spatial complexity : O ( d × m ) O(d×m) O(d×m), The cost of sorting .
Method 3 : Double pointer +DP Preprocessing
Ideas
On the basis of method one , We consider by pairing strings s s s Preprocessing , To optimize the 1 1 1 How to deal with these problems .
Consider the previous double pointer approach , We notice that we have a lot of time for s s s Next matching character found in .
In this way, we can preprocess , obtain : For strings s s s Every position of , Start at the first position of the character ( Including the location ).
We can use the method of dynamic programming to realize preprocessing , Make f [ i ] [ j ] f[i][j] f[i][j] Representation string s s s From the middle i i i Start with the following characters j j j First occurrence . During state transition , If s s s Middle position i i i The character is j j j, that f [ i ] [ j ] = i f[i][j]=i f[i][j]=i, otherwise j j j Appear in position i + 1 i+1 i+1 Start back , namely f [ i ] [ j ] = f [ i + 1 ] [ j ] f[i][j]=f[i+1][j] f[i][j]=f[i+1][j]; So we have to reverse the dynamic planning , Enumerate from back to front i i i.
So we can write State transition equation :
f [ i ] [ j ] = { i , s [ i ] = j f [ i + 1 ] [ j ] , s [ i ] ≠ j f[i][j]= \begin{cases} i, & s[i]=j \\ f[i+1][j], & s[i] \ne j \end{cases} f[i][j]={ i,f[i+1][j],s[i]=js[i]=j
Assume that the subscript is from 0 0 0 Start , that f [ i ] [ j ] f[i][j] f[i][j] There is 0 ≤ i ≤ m − 1 0 \leq i \leq m-1 0≤i≤m−1, For boundary States f [ m − 1 ] [ . . ] f[m-1][..] f[m−1][..], We make f [ m ] [ . . ] f[m][..] f[m][..] The value of is m m m, Give Way f [ m − 1 ] [ . . ] f[m-1][..] f[m−1][..] Transfer normally . So if f [ i ] [ j ] = m f[i][j]=m f[i][j]=m, From position i i i No characters after start j j j.
such , We can use f f f Array , Every time O ( 1 ) O(1) O(1) Jump to the next position , Until the position changes to m m m or t t t Every character in is matched successfully .
Code implementation
Leetcode Official explanation :
class Solution {
public:
string findLongestWord(string s, vector<string>& dictionary) {
const int n = s.size();
int next[26];
fill_n(next, 26, -1);
int trans[n + 1][26];
fill_n(trans[n], 26, -1);
for (int i = n - 1;0 <= i;--i) {
next[s[i] - 'a'] = i + 1;
copy_n(next, 26, trans[i]);
}
string_view ans = "";
for (const auto& e : dictionary) {
int state = 0;
for (char c : e) {
state = trans[state][c - 'a'];
if (state == -1) break;
}
if (state == -1) continue;
if (e.size() > ans.size() || e.size() == ans.size() && e < ans)
ans = e;
}
return string(ans);
}
};
My own (STL edition ):
class Solution {
public:
string findLongestWord(string s, vector<string>& dictionary) {
int maxL = -1, ptr, s_size = s.size(), d_size = dictionary.size();
vector<vector<int>> s_letter(s_size, vector<int>(26, -1));
for(int n = s_size-1; n >= 0; n--){
if(n != s_size-1) s_letter[n] = s_letter[n+1];
s_letter[n][s[n]-'a'] = n;
}
for(int n = 0; n < d_size; n++){
if(maxL != -1 && (dictionary[n].size() < dictionary[maxL].size() ||
(dictionary[n].size() == dictionary[maxL].size() && dictionary[n] >= dictionary[maxL]))) continue;
ptr = 0;
for(int i = 0; i < s_size && ptr < dictionary[n].size(); i++){
i = s_letter[i][dictionary[n][ptr]-'a'];
if(i == -1) break;
ptr++;
}
if(ptr == dictionary[n].size()) maxL = n;
}
return maxL == -1? "": dictionary[maxL];
}
};
My own ( Basic array type ):
class Solution {
public:
string findLongestWord(string s, vector<string>& dictionary) {
int maxL = -1, ptr, s_size = s.size(), d_size = dictionary.size();
int s_letter[s_size][26];
fill_n(s_letter[s_size-1], 26, -1); // Just assign values to the last array , There is DP It will be assigned to other arrays during processing
for(int n = s_size-1; n >= 0; n--){
if(n != s_size-1) copy_n(s_letter[n+1], 26, s_letter[n]);
s_letter[n][s[n]-'a'] = n;
}
for(int n = 0; n < d_size; n++){
if(maxL != -1 && (dictionary[n].size() < dictionary[maxL].size() ||
(dictionary[n].size() == dictionary[maxL].size() && dictionary[n] >= dictionary[maxL]))) continue;
ptr = 0;
for(int i = 0; i < s_size && ptr < dictionary[n].size(); i++){
i = s_letter[i][dictionary[n][ptr]-'a'];
if(i == -1) break;
ptr++;
}
if(ptr == dictionary[n].size()) maxL = n;
}
return maxL == -1? "": dictionary[maxL];
}
};
Complexity analysis
Time complexity : O ( m × ∣ Σ ∣ + d × n ) O(m \times |\Sigma|+d \times n) O(m×∣Σ∣+d×n), among d d d Express dictionary \textit{dictionary} dictionary The length of , Σ \Sigma Σ Is a character set , In this question, the string only contains English lowercase letters , so ∣ Σ ∣ = 26 |\Sigma|=26 ∣Σ∣=26; m m m Representation string s s s The length of , n n n Express dictionary \textit{dictionary} dictionary Average length of string in . The time complexity of preprocessing is O ( m × ∣ Σ ∣ ) O(m \times |\Sigma|) O(m×∣Σ∣); Judge d d d Whether the string is s s s The event complexity of the subsequence of is O ( d × n ) O(d \times n) O(d×n).
Spatial complexity : O ( m × ∣ Σ ∣ ) O(m×∣\Sigma∣) O(m×∣Σ∣), To dynamically plan the cost of arrays .
边栏推荐
- Machine learning 10 belief Bayesian classifier
- AS400 大廠面試
- Fast understanding of forward proxy and reverse proxy
- Thinking about business and investment
- 【做题打卡】集成每日5题分享(第一期)
- 工作八年的程序员,却拿着毕业三年的工资,再不开窍就真晚了...
- AS400 entretien d'usine
- Laravel event & subscription
- Alphabet-Rearrange-Inator 3000(字典树自定义排序)
- What are the preferential activities for stock account opening? In addition, is it safe to open a mobile account?
猜你喜欢

Delete duplicate email

Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)
![Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]
![[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大](/img/fa/f9bad44147ba9af21183b7bd630e32.png)
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大

软件开发中的上游和下游

Selenium经典面试题-多窗口切换解决方案

【JS】【掘金】获取关注了里不在关注者里的人

Upstream and downstream in software development

Batch import of Excel data in applet

7-2 拼题A打卡奖励 dp
随机推荐
C # customize and dynamically switch cursor
【毕业季·进击的技术er】--毕业到工作小结
Opencv -- Notes
Mathematical knowledge: 01 sequence satisfying conditions - find combinatorial number
PHP数组拼接MySQL的in语句
php将二维数组元素转为键值对
The latest CSDN salary increase technology stack in 2022 overview of APP automated testing
SWT/ANR问题--Native方法执行时间过长导致SWT
522. 最长的特殊序列 II
Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)
Fix names in the table (first character uppercase, other lowercase)
Int and bit group turn to each other
小程序中实现excel数据的批量导入
如何选择券商?另外,手机开户安全么?
laravel Carbon 时间处理类使用
More pragmatic in business
What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss
Check the disk usage of MySQL database
Ks009 implementation of pet management system based on SSH
Test essential tool - postman practical tutorial