当前位置:网站首页>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
- How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
- 招标信息获取
- 知识沉淀一:架构师是做什么?解决了什么问题
- Sequential search, half search, block search~
- Establishment of log collection and analysis platform-1-environment preparation
- Mysql45 talking about infrastructure: how is an SQL query executed?
- Unity2d animator cannot create transition
- PHP 多任务秒级定时器的实现方法
- Etcd database source code analysis - cluster membership changes log
猜你喜欢

Kingbasees SQL language reference manual of Jincang database (8. Function (10))

递归函数中 有两个递归入口的时间复杂度

移动web

H. Take the Elevator 贪心

漫谈软件缺陷管理的实践

Introduction of four redis cluster schemes + comparison of advantages and disadvantages

How to divide the disks under the devices and drives in win10 new computer

Convolutional neural network (II) - deep convolutional network: case study

Easycvr video square channel display and video access full screen display style problem repair

Docking wechat payment (II) unified order API
随机推荐
Sequential search, half search, block search~
金仓数据库 KingbaseES SQL 语言参考手册 (9. 常见DDL子句)
Full binary tree / true binary tree / complete binary tree~
PHP 多任务秒级定时器的实现方法
Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
Practice operation and maintenance knowledge accumulation
102. (cesium chapter) cesium road streamer
招标信息获取
Knowledge precipitation I: what does an architect do? What problems have been solved
[free and easy to use] holiday query interface
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
Mysql45 speak in simple terms index
Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
Solution to slow download speed of vagrant
ament_ Cmake generates the ros2 library and links it
CV (1)- Introduction
Ros2 knowledge: DDS basic knowledge
日志收集分析平台搭建-1-环境准备
Mba-day29 arithmetic - preliminary understanding of absolute value
NFT in the eyes of blackash: the platform is crying for slaughter, and users send money to the door