Can u plz send this code

not able to implement plz help by this method

Steps:

  1. Traverse the array, build map. Key is the reversed string, value is index in array (0 based)
  2. Edge case - check if empty string exists. It’s interesting that for given words {β€œa”, β€œβ€}, it’s expected to return two results [0,1] and [1,0]. Since my main logic can cover [0, 1] concatenate(β€œa”, β€œβ€), so as to cover the other situation concatenate("", β€œa”), I need to traverse the words array again, find the palindrome word candidate except β€œβ€ itself, and add pair("", palindrome word) to the final answer.
  1. Main logic part. Partition the word into left and right, and see 1) if there exists a candidate in map equals the left side of current word, and right side of current word is palindrome, so concatenate(current word, candidate) forms a pair: left | right | candidate . 2) same for checking the right side of current word: candidate | left | right .
     vector<vector<int>> palindromePairs(vector<string>& words) {
         unordered_map<string, int> dict;
         vector<vector<int>> ans;
         // build dictionary
         for(int i = 0; i < words.size(); i++) {
             string key = words[i];
             reverse(key.begin(), key.end());
             dict[key] = i;
         }
         // edge case: if empty string "" exists, find all palindromes to become pairs ("", self)
         if(dict.find("")!=dict.end()){
             for(int i = 0; i < words.size(); i++){
                 if(i == dict[""]) continue;
                 if(isPalindrome(words[i])) ans.push_back({dict[""], i}); // 1) if self is palindrome, here ans covers concatenate("", self) 
             }
         }

         for(int i = 0; i < words.size(); i++) {
             for(int j = 0; j < words[i].size(); j++) {
                 string left = words[i].substr(0, j);
                 string right = words[i].substr(j, words[i].size() - j);

                 if(dict.find(left) != dict.end() && isPalindrome(right) && dict[left] != i) {
                     ans.push_back({i, dict[left]});     // 2) when j = 0, left = "", right = self, so here covers concatenate(self, "")
                 }

                 if(dict.find(right) != dict.end() && isPalindrome(left) && dict[right] != i) {
                     ans.push_back({dict[right], i});
                 }
             }
         }

         return ans;        
     }

     bool isPalindrome(string str){
         int i = 0;
         int j = str.size() - 1; 

         while(i < j) {
             if(str[i++] != str[j--]) return false;
         }

         return true;
     }

1 Like

ans.push_back({dict[right], i});
what this line doing sir?

it push backs a vector of size 2 with first element as dict[right] and second element as i

how to push back in 2d vector?

its same as pushing a variable in a vector only difference is this time we are pushing a vector.
what i mean is vector<vector > means a vector of vectors so we can simply use vector::push_back() method to push back an array

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.