Before and After Puzzle in C++

C++Server Side ProgrammingProgramming

Suppose we have a list of phrases, generate a list of Before and After puzzles. Here a phrase is a string that consists of lowercase letters and spaces only. No space will be there at the starting and ending positions. There are no consecutive spaces in a phrase.

The before and after puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase. We have to find the Before and After puzzles that can be formed by every two phrases phrases[i] and phrases[j] where I!= j. Note that the order of matching two phrases matters, we want to consider both orders.

We should find a list of distinct strings sorted lexicographically. So if the input is like phrases = ["mission statement", "a quick bite to eat", "a chip off the old block", "chocolate bar", "mission impossible", "a man on a mission", "block party", "eat my words", "bar of soap"], then the output will be: ["a chip off the old block party", "a man on a mission impossible", "a man on a mission statement", "a quick bite to eat my words", "chocolate bar of soap"].

To solve this, we will follow these steps −

  • Define an array of strings ret, sort the phrases array

  • define a map m, n := size of phrases array

  • for I in range 0 to n – 1

    • s := phrases[i], rspace := index of blank spaces from the right side

    • insert I into the list placed at m[when rspace is null, then s, otherwise find substring of s up to rspace + 1]

  • for I in range 0 to n – 1

    • s := phrases[i] lspace := index of blank spaces from the left side

    • x := when lspace is null, then s, otherwise find substring of s from 0 to lspace]

    • if m has x as key

      • v := m[x]

      • for j in range 0 to size of v

        • if v[j] is not I, then

          • insert phrases[v[j]] + substring of s(up to size of x) into ret

  • sort ret

  • delete unique of ret and return ret

Example (C++)

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
public:
   vector<string> beforeAndAfterPuzzles(vector<string>& phrases) {
      vector <string> ret;
      sort(phrases.begin(), phrases.end());
      unordered_map <string, vector <int> > m;
      int n = phrases.size();
      for(int i = 0; i < n; i++){
         string s = phrases[i];
         auto rspace = s.rfind(' ');
         m[rspace == string::npos ? s : s.substr(rspace + 1)].push_back(i);
      }
      for(int i = 0; i < n; i++){
         string s = phrases[i];
         auto lspace = s.find(' ');
         string x = (lspace == string::npos? s : s.substr(0, lspace));
         if(m.count(x)){
            vector <int>& v = m[x];
            for(int j = 0; j < v.size(); j++){
               if(v[j] != i){
                  ret.push_back(phrases[v[j]] + s.substr(x.size()));
               }
            }      
         }
      }
      sort(ret.begin(), ret.end());
      ret.erase(unique(ret.begin(), ret.end()), ret.end());
      return ret;
   }
};
main(){
   vector<string> v = {"mission statement","a quick bite to eat","a chip off the old block","chocolate bar","mission impossible","a man on a mission","block party","eat my words","bar of soap"};
   Solution ob;
   print_vector(ob.beforeAndAfterPuzzles(v));
}

Input

["mission statement","a quick bite to eat","a chip off the old block","chocolate bar","mission impossible","a man on a mission","block party","eat my words","bar of soap"]

Output

[a chip off the old block party, a man on a mission impossible, a man on a mission
statement, a quick bite to eat my words, chocolate bar of soap, ]
raja
Published on 31-Mar-2020 09:02:48
Advertisements