Word Pattern II in C++


Suppose we have a pattern and a string called str, we have to check whether str follows the same pattern or not. Here pattern follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.

So, if the input is like pattern is "abaa", str is "orangegreenorangeorange", then the output will be true

To solve this, we will follow these steps −

  • Define a function solve(), this will take i, j, ptr, s, a map m, one set called used,

  • if i >= size of s and j >= size of ptr, then −

    • return true

  • if i >= size of s or j >= size of ptr, then −

    • return false

  • if ptr[j] is in m, then −

    • req := m[ptr[j]]

    • len := size of req

    • if len > size of s, then −

      • return false

    • if substring of s from index (i to len-1) is same as req and solve(i + len, j + 1, ptr, s, m, used), then −

      • return true

    • return false

  • Otherwise

    • x := ptr[j]

    • for initialize k := i, when k < size of s, update (increase k by 1), do −

      • temp := substring of s from index (i to k - i)

      • if temp is in used, then −

        • Ignore following part, skip to the next iteration

      • m[x] := temp

      • insert temp into used

      • if solve(k + 1, j + 1, ptr, s, m, used), then −

        • return true

      • delete x from m

      • delete temp from used

  • return false

  • From the main method do the following −

  • Define one map m

  • Define one set used

  • return solve(0, 0, ptr, s, m, used)

Example 

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

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   bool solve(int i, int j, string ptr, string s, map <char, string>& m, set<string>& used){
      if (i >= s.size() && j >= ptr.size()) {
         return true;
      }
      if (i >= s.size() || j >= ptr.size())
         return false;
      if (m.count(ptr[j])) {
         string req = m[ptr[j]];
         int len = req.size();
         if (len > s.size() - i)
            return false;
         if ((s.substr(i, len) == req) && solve(i + len, j + 1, ptr, s, m, used))
            return true;
         return false;
      }
      else {
         char x = ptr[j];
         for (int k = i; k < s.size(); k++) {
            string temp = s.substr(i, k - i + 1);
            ;
            if (used.count(temp))
               continue;
            m[x] = temp;
            used.insert(temp);
            if (solve(k + 1, j + 1, ptr, s, m, used))
               return true;
            m.erase(x);
            used.erase(temp);
         }
      }
      return false;
   }
   bool wordPatternMatch(string ptr, string s) {
      map<char, string> m;
      set<string> used;
      return solve(0, 0, ptr, s, m, used);
   }
};
main(){
   Solution ob;
   cout << (ob.wordPatternMatch("abaa", "orangegreenorangeorange"));
}

Input

"abaa" "orangegreenorangeorange"

Output

1

Updated on: 21-Jul-2020

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements