LeetCode 336. Palindrome Pairs

本文介绍了一种算法,用于从给定的唯一单词列表中找到所有可能形成回文串的单词对。通过两种不同的实现方式,展示了如何有效地解决这个问题,并讨论了各自的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <vector>
#include <string>
#include <iostream>
#include <unordered_map>
using namespace std;

/*
  Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list.
  So that the concatenation of two words. ie, words[i] + words[j] is a palindrome.
  Example:
    Given words = ["abcd", "dcba", "lls", "s", "sssll"]
    return [[0, 1], [1, 0], [3, 2], [2, 4]]
    The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]
*/
bool isPalindrome(string s) {
  if(s.size() <= 1) return true;
  int i = 0;
  int j = s.size()-1;
  while(i <= j) {
    if(s[i] != s[j]) return false;
    i++;
    j--;
  }
  return true;
}
// Time complexity O(N^2).... too high....
vector< vector<int> > palindromePairs(vector<string>& words) {
  vector< vector<int> > res;
  unordered_map<string, int> stringToIndex;
  for(int i = 0; i < words.size(); ++i) {
    stringToIndex.insert({words[i], i});
  }
  for(int i = 0; i < words.size(); ++i) {
    auto iter = stringToIndex.begin();
    while(iter != stringToIndex.end()) {
      vector<int> tmp;
      if(iter->first == words[i]) {iter++; continue;}
      if(isPalindrome(iter->first + words[i])) {
        tmp.push_back(iter->second);
        tmp.push_back(i);
        res.push_back(tmp);
      }
      iter++;
    }
  }
  return res;
}

bool isPalindrome(string s, int start, int end) {
  if(start > end) return false;
  while(start <= end) {
    if(s[start] == s[end]) {start++; end--; continue;}
    return false;
  }
  return true;
}

// Get the idea from LeetCode forum. Time Complexity O(NK), Space O(N), it currently wont run.....;(
vector< vector<int> > palindromePairsII(vector<string>& words) {
  unordered_map<string, int> hashMap;
  vector< vector<int> > res;
  for(int i = 0; i < words.size(); ++i) {
    hashMap.insert({words[i], i});
  }
  for(int i = 0; i < words.size(); ++i) {
    string curr = words[i];
    int n = curr.size();
    for(int l = 0; l < n; ++i) {
      if(isPalindrome(curr, 0 , l - 1)) {
        string tmp = curr.substr(l);
        if(hashMap.count(tmp)) {
          int k = hashMap[tmp];
          if(i != k) res.push_back(vector<int>{i, k});
        }
      }
      if(isPalindrome(curr, l, n - 1)) {
        string tmp = curr.substr(0, n-l);
        if(hashMap.count(tmp)) {
          int k = hashMap[tmp];
          if(i != k) res.push_back(vector<int>{i, k});
        }
      }
    }
  }
  return res;
}

int main(void) {
  vector<string> words{"abcd", "dcba", "lls", "s", "sssll"};
  vector< vector<int> > res = palindromePairs(words);
  for(int i = 0; i < res.size(); ++i) {
    for(int j = 0; j < res[i].size(); ++j) {
      cout << res[i][j] << " ";
    }
    cout << endl;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值