当前位置:网站首页>Leetcode 336 palindrome pair (palindrome string + hash)
Leetcode 336 palindrome pair (palindrome string + hash)
2022-06-12 09:03:00 【Little wish】
The main idea of the topic : Give me a set of strings , Output all the two string splicing can form a palindrome string combination
The sample input :[“abcd”,“dcba”,“lls”,“s”,“sssll”]
Sample output :[[0,1],[1,0],[3,2],[2,4]]
Ideas : Enumerate all possible splices , Determine whether each splicing is a palindrome string ,TLE.
Violence code :
class Solution {
private:
bool isHuiWen(string a){
int i = 0, j = a.size()-1;
while (i < j){
if (a[i] != a[j]) return false;
i++;j--;
}
return true;
}
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> ans;
int len = words.size();
for (int i = 0; i < len; i++){
for (int j = i+1; j < len; j++){
if (isHuiWen(words[i]+words[j])){
vector<int> t;t.push_back(i);t.push_back(j);
ans.push_back(t);
}
if (isHuiWen(words[j]+words[i])){
vector<int> t;t.push_back(j);t.push_back(i);
ans.push_back(t);
}
}
}
return ans;
}
};
Optimization idea : Suppose the concatenated string is a , b a,b a,b, Might as well set l e n ( a ) > = l e n ( b ) len(a)>=len(b) len(a)>=len(b), If a a a and b b b The spliced string is a palindrome string , So the string a a a Before l e n ( b ) len(b) len(b) String is a string b b b The transpose , And a a a The remaining strings are also palindromes .
Implementation method : Use m a p < s t r i n g , i n t > map<string,int> map<string,int> Record all strings and subscripts , For each string , Determine whether each substring is a palindrome string , Whether the transpose of the remaining part is the same as other strings
AC Code :
class Solution {
private:
map<string, int> mp;
bool isHuiWen(string s, int a, int b){
int i = a, j = b;
while (i < j){
if (s[i] != s[j]) return false;
i++;j--;
}
return true;
}
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> ans;
int len = words.size();
for (int i = 0; i < len; i++){
mp[words[i]] = i;
}
for (int i = 0; i < len; i++){
int m = words[i].size();
for (int j = 0; j <= m; j++){
if (isHuiWen(words[i], j, m-1)){
string t = words[i].substr(0, j);
reverse(t.begin(), t.end());
if (mp.count(t) && mp[t] != i){
ans.push_back(vector<int>{
i, mp[t]});
}
}
if (j && isHuiWen(words[i], 0, j-1)){
string t = words[i].substr(j, m);
reverse(t.begin(), t.end());
if (mp.count(t) && mp[t] != i){
ans.push_back(vector<int>{
mp[t], i});
}
}
}
}
return ans;
}
};
边栏推荐
- Wechat applet image saving function
- UMI packaging and subcontracting, and compressing to gzip
- Node sample background setup
- Building a cluster: and replacing with error
- Close asymmetric key
- Shell basic syntax -- array
- sql中的Exists用法
- ip、DNS、域名、URL、hosts
- Method to limit the input box to only numbers
- 2024. 考试的最大困扰度-滑动窗口
猜你喜欢

Xshell startup encountered "unable to continue code execution because mfc110.dll cannot be found"

Background location case 1

Does database and table splitting cause reading diffusion problems? How to solve it?

Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low

Load the source code of 2D 3D virtual anchor in the web page (1: project introduction and source code)

MFS explanation (IV) -- MFS management server installation and configuration

Union selector

Summary of common character sets

Background color translucent

2022 melting welding and thermal cutting test questions and answers
随机推荐
Machine learning notes - circular neural network memo list
MySQL learning records -- III. MySQL query statements
【无标题】Task3 多路召回
JS to refresh the page after loading
解决当打开Unity时 提示项目已经打开,而自己之前并没有打开过(可能之前异常关闭)的问题
Sword finger offer II 016 Longest substring without repeating characters - sliding window
node示例后台搭建
Introduction to applet cloud development -- questionnaire evaluation applet practice (7)
【字符集八】char8_t、char16_t、char32_t、wchar、char
Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
Method to limit the input box to only numbers
Introduction to Chang'an chain node certificate, role and authority management
MySQL learning record - II. MySQL create table command
Background attribute compound writing
Xshell startup encountered "unable to continue code execution because mfc110.dll cannot be found"
[advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function
Jenkins Pipeline 语法
【字符集六】宽字符串和多字节字符互转
(14) Inputfield logic analysis
day5-x