Leetcode 336. Palindrome Pairs

Source: Internet
Author: User

Transmission Door

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so, the concatenation of the-words, i.e. is a palindrome.

Example 1:
Given words =["bat", "tab", "cat"]
Return[[0, 1], [1, 0]]
The palindromes is["battab", "tabbat"]

Example 2:
Given words =["abcd", "dcba", "lls", "s", "sssll"]
Return[[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes is["dcbaabcd", "abcddcba", "slls", "llssssll"]

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide TagsHash Table String TrieShow Similar Problems turn round: http://blog.csdn.net/liu_460660596/article/details/51034943

Test instructions: Give a dictionary with n words, ask how many different combinations of two words make the new combination of words is palindrome string.

Idea: For each word, from left to right to iterate over, for any position, if its left is a palindrome string and the right side of the reverse in the dictionary appears, then there is such a combination, similarly, if its right is a palindrome string and the left side of the reverse in the dictionary appears, then there is such a combination.

It's a good question, it involves a lot of knowledge.

But there is a small doubt, why use a map to time out, with Unordered_map on the

chaotic map of Stl::unordered_map

http://blog.csdn.net/devourheavens/article/details/7539815

A hash map is an associative container that stores elements through key values and mapped values . Allows quick retrieval of individual elements based on key values.

In Unordered_map, a key value is typically used to uniquely identify an element, and the corresponding value is the content of an object associated with that key. The type of the key mapping value may vary. Elements in the internal unordered_mapNoAn element with a key value or a mapmake any particular sort of orderItsThe storage location depends on the hash value allowing for fast access to a single element (with a constant average time complexity) directly through its key value. UNORDERED_MAP container than Map containeraccess their individual elements faster with key values, although unordered_map is generally more than a subset of the map through its elements rangeLow Iteration efficiency。 Hash map allows you to use the operator operator (operator []) to directly access an element with its key value as a parameter.

Submission Details
134/134 Test cases passed. status:accepted
runtime:1804 ms

1 classSolution {2  Public:3vector<vector<int>> Palindromepairs (vector<string>&words) {4vector<vector<int> >ans;5unordered_map<string,int>MP;6         intn =words.size ();7         inti,j;8          for(i =0; I < n;i++){9mp[Words[i]] =i;Ten         } One          A          for(i =0; I < n;i++){ -             intL =words[i].length (); -             if(L = =0){//empty string the                  for(intK =0; k < n;k++)   -                     if(k! = I && ISPA (Words[k])) Ans.push_back (vector<int>{i,k});  -                 Continue; -             }   +Mp.erase (Words[i]);//It is a palindrome, which can cause errors. -              for(j =0; J < L;j + +){   +                 stringSubl = Words[i].substr (0, J);  A                 stringSUBR =words[i].substr (j,l);  at                 stringREVL,REVR;  - revl.assign (Subl.rbegin (), Subl.rend ());  - revr.assign (Subr.rbegin (), Subr.rend ());  -                 //J Left is a palindrome, and J right side is found in reverse order (including J) -                 if(Mp.find (REVR)! = Mp.end () &&ISPA (SUBL)) {  -Ans.push_back (vector<int>{mp[revr],i}); in                 } -                 //J Right is a palindrome, and J left side is found in reverse order (including J) to                 if(Mp.find (REVL)! = Mp.end () &&ISPA (SUBR)) { +Ans.push_back (vector<int>{I,MP[REVL]}); -                 } the             } *mp[Words[i]] =i; $         }Panax Notoginseng         returnans; -     } the     BOOLIspastring&s) +     { A         inti =0; the         intL =s.length (); +         intj = L-1; -          while(I <=j) { $             if(S[i]! = S[j])return false; $i++;j--; -         } -         return true; the     } -};

string of C++stl

http://blog.csdn.net/y990041769/article/details/8763366

Http://www.cnblogs.com/aicro/archive/2010/01/15/1642659.html

A) =,assign ()//Assign new value

S.assign (b,e); Replace s with elements in the iterator B to e range

Introduction to common operation of STL map

Http://www.cnblogs.com/TianFang/archive/2006/12/30/607859.html

7. Delete an element from the map

Remove an entry from a map with erase ()

The member method is defined as follows

    1. Iterator Erase (iterator it); Delete by an Entry object
    2. Iterator Erase (iterator first, iterator last); Delete a range
    3. Size_type Erase (const key& Key); Delete by keyword

Clear () is equivalent to Enummap.erase (Enummap.begin (), Enummap.end ());

Leetcode 336. Palindrome Pairs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.