当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Fast understanding of forward proxy and reverse proxy

AS400 API 从零到一的整个历程

How does ZABBIX configure alarm SMS? (alert SMS notification setting process)

Connectivity basis of Graphs

electron之坑addon

Test essential tool - postman practical tutorial
The latest CSDN salary increase technology stack in 2022 overview of APP automated testing

计算特殊奖金

Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again

数学知识:求组合数 III—求组合数
随机推荐
How to learn and read code
VirtualBox 安装增强功能
QT web 开发 - video -- 笔记
【Content-Type请求头的内容】
Laravel+redis generates an order number - automatically increase from 1 on the same day
Last day of the second quarter
端口号和进程号的区别?
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大
Electron pit Addon
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
CorelDRAW 2022中文精简64位直装版下载
[fundamentals of wireless communication-14]: illustrated mobile communication technology and application development-2-the first generation mobile analog communication big brother
The personal test is effective, and the JMeter desktop shortcut is quickly created
SWT/ANR问题--AMS/WMS
There is no future to be expected. It is just the last fantasy of a migrant worker before he dies
Handsontable数据网格组件
QT web development - VIDEO - Notes
7-2 punch in reward DP for puzzle a
LabVIEW计算相机图像传感器分辨率以及镜头焦距
[content of content type request header]