当前位置:网站首页>Leetcode:336. palindrome pair
Leetcode:336. palindrome pair
2022-07-26 06:06:00 【Oceanstar's study notes】
Title source
Title Description

class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
}
};
title
Analyze the amount of data

If violence matches : 15 ∗ 1 0 5 15 * 10^5 15∗105, Can pass

Can I hash bucket count ?
Analyze the meaning of the question
from words Select any two words in the array , See if it can form a palindrome string , And return an index array that can form a palindrome string .
Be careful [0,1] and [1,0] It's different
violence ( Overtime )
Exhausting i , j i,j i,j, Find all possible combinations . If it is a palindrome , Just put the corresponding ( i , j ) (i,j) (i,j) Push the result array .
Time complexity O ( ( N 2 ) ∗ L ) O((N^2) * L) O((N2)∗L), N N N It's the number of words , L L L Is the average length of words
class Solution {
bool isPalindrome(std::string str){
if(str.empty()){
return true;
}
int i = 0, j = (int)str.size() - 1;
while (i <= j){
if(str[i] != str[j]){
return false;
}
i++;
j--;
}
return true;
}
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> res;
int N = words.size();
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if(i != j){
if (isPalindrome(words[i] + words[j])) res.push_back({
i, j});
}
}
}
return res;
}
};
class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> result{
};
int const size = words.size();
for (int i = 0; i < size; ++i)
for (int j = i + 1; j < size; ++j)
{
if (ispalindrome(words[i], words[j]))
result.push_back(vector<int>({
i,j}));
if (ispalindrome(words[j], words[i]))
result.push_back(vector<int>({
j,i}));
}
return result;
}
private:
bool ispalindrome(string const & word_left, string const & word_right) {
int const sizel = word_left.size();
int const sizer = word_right.size();
int l = 0;
int r = sizel + sizer - 1;
while (l < r) {
char left = (l < sizel) ? (word_left[l]) : (word_right[l - sizel]) ;
char right = (r < sizel) ? (word_left[r]) : (word_right[r - sizel]);
if (left != right) return false;
++l, --r;
}
return true;
}
};
Enumerate prefixes and suffixes


- In order to find flipped words quickly , Let's turn the words over in advance , Save to hash table , And its corresponding index .
class Solution {
bool isPalindrome(std::string str){
if(str.empty()){
return true;
}
int i = 0, j = (int)str.size() - 1;
while (i <= j){
if(str[i] != str[j]){
return false;
}
i++;
j--;
}
return true;
}
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
int N = words.size();
std::map<std::string, int> reverse_map;
for (int i = 0; i < N; ++i) {
auto word = words[i];
std::reverse(word.begin(), word.end());
reverse_map.insert({
word, i});
}
vector<vector<int>> res;
for (int i = 0; i < N; ++i) {
std::string word = words[i];
// Think of yourself as palindrome , There is "" when , It can form two pairs
auto it = reverse_map.find("");
if(it != reverse_map.end() && (reverse_map[""] != i) && isPalindrome(word)){
res.emplace_back(vector<int>{
reverse_map[""], i});
res.emplace_back(vector<int>{
i, reverse_map[""]});
}
// When you are not palindrome , You can form a pair with yourself in reverse order
if(!isPalindrome(word)){
// abcd ----> dcba ---> abcd
auto tmp = words[i];
if(reverse_map.find(tmp) != reverse_map.end() && reverse_map[tmp] != i){
res.emplace_back(vector<int>{
i, reverse_map[tmp]});
}
}
// Enumerate prefixes and suffixes
for (int j = 1; j <= word.size() - 1; ++j) {
// Split
auto left = word.substr(0, j); // The left part
auto right = word.substr(j); // The right part
//fb + [aa]bf
if(isPalindrome(left) && reverse_map.find(right) != reverse_map.end() && reverse_map[right] != i){
res.emplace_back(vector<int>{
reverse_map[right], i}); // Palindrome on the left , On the right, you can find the flip and not yourself
}
// af[cc] + fa
if(isPalindrome(right) && reverse_map.find(left) != reverse_map.end() && reverse_map[left] != i){
res.emplace_back(vector<int>{
i, reverse_map[left]}); // Palindrome on the left , On the right, you can find the flip and not yourself
}
}
}
return res;
}
};
边栏推荐
- Implementation of PHP multitask second timer
- Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
- Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
- Binary sort tree (BST)~
- Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
- Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
- Mba-day29 arithmetic - preliminary understanding of absolute value
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
- Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
- “子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
猜你喜欢

Redis master-slave replication

Using easyexcel to import tables to realize batch insertion of xlsx files ----- MySQL of Linux

2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers

Mysql45 talks about transaction isolation: why can't I see it after you change it?

某公司给每个工位装监控:只为看员工写代码?

Mysql45 talking about global lock, table lock and row lock

Unity2D 动画器无法 创建过渡

Implementation of PHP multitask second timer

Modifiers should be declared in the correct order 修饰符应按正确的顺序声明

Viewing the technology stack of distributed system from the crash report of station B
随机推荐
WebAPI整理
[Oracle SQL] calculate year-on-year and month on month (column to row offset)
Redis master-slave replication
Database SQL language practice
二叉树的性质 ~
Redis persistence RDB
L. Link with Level Editor I dp
Understanding the mathematical essence of machine learning
金仓数据库 KingbaseES SQL 语言参考手册 (9. 常见DDL子句)
顺序查找,折半查找,分块查找 ~
VRRP protocol and experimental configuration
[paper notes] anti packet loss joint coding for network speech steganography
vagrant下载速度慢的解决方法
Ganglia installation and deployment process
Mysql45 speak in simple terms index
Learn about spark project on nebulagraph
2022年下半年系统集成项目管理工程师(软考中级)报名条件
Mba-day28 concept of number - exercise questions
"Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十))