The Wayback Machine - https://web.archive.org/web/20221207122156/https://www.geeksforgeeks.org/check-if-string-t-can-be-made-substring-of-s-by-replacing-given-characters/
Skip to content
Related Articles

Related Articles

Check if String T can be made Substring of S by replacing given characters

Improve Article
Save Article
  • Last Updated : 07 Dec, 2022
Improve Article
Save Article

Given two strings S and T and a 2D array replace[][], where replace[i] = {oldChar, newChar} represents that the character oldChar of T is replaced with newChar. The task is to find if it is possible to make string T a substring of S by replacing characters according to the replace array. 

Note: Each character in T cannot be replaced more than once.

Example:

Input: S = “hoog3e7bar”, T = “geek”, replace = {{‘e’, ‘3’}, {‘t’, ‘7’}, {‘k’, ‘8’}}
Output: true
Explanation: Replace the first ‘e’ in T with ‘3’ and ‘k’ in T with ‘7’.
Now T = “g3e7” is a substring of S, so we return true.

Input: S = “gooleetbar”, T = “g00l”, replace = {{‘o’, ‘0’}}
Output: false

Approach: The problem can be solved using Hashing based on the following idea:

Keep a datastructure for storing all the mapping of replace array. Enumerate all substrings of S with the same length as T, and compare each substring to S for equality.

Follow the steps below to implement the above approach:

  • Initialize a map say unmap, for mapping all the characters that oldChar can replace with newChar.
  • Iterate over the replace array and do the mapping.
  • Iterate over all the subarray of S of length equals to given string T.
    • Check if the character in S is not equal to the character in T.
      • If not equal then check if there exists any replacement of T[j] in unmap.
      • If a mapping exists then continue.
      • Otherwise, break from the current iteration and look for the other substring.
  • If we successfully find a valid substring, then return true.
  • Otherwise, return false.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if the string
// can be made substring of S
bool match(string S, string T,
           vector<vector<char> >& replace)
{
    // Initialise a map say unmap, for
    // mapping all the characters that
    // oldChar can replace with newChar
    unordered_map<char, unordered_set<char> > unmap;
    int m = S.size(), n = T.size();
  
    // Iterate over the replace array
    // and map in unmap
    for (auto c : replace) {
        unmap].insert(c[1]);
    }
  
    // Iterate over all the subarray in S
    // of length equals to given string T.
    for (int i = 0; i < m - n + 1; i++) {
        bool flag = true;
        for (int j = 0; j < n; j++) {
  
            // Check if the character in S
            // is not equal to character
            // in T. If not equal then
            // check if there exist any
            // replacement of T[j] in unmap.
            if (S[i + j] != T[j]) {
  
                // If mapping exists, continue
                if (unmap[T[j]].find(S[i + j])
                    != unmap[T[j]].end()) {
                    continue;
                }
  
                // Otherwise, break from
                // current iteration and look
                // for the another substring.
                else {
                    flag = false;
                    break;
                }
            }
        }
  
        // If we successfully found valid
        // substring, then return true
        if (flag)
            return true;
    }
  
    // Otherwise, return false
    return false;
}
  
// Drivers code
int main()
{
    string S = "hoog3e7bar", T = "geek";
    vector<vector<char> > replace
        = { { 'e', '3' }, { 't', '7' }, { 'k', '7' } };
  
    // Function call
    bool result = match(S, T, replace);
  
    if (result)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
  
    return 0;
}

Output

YES

Time Complexity: O(N * M) where N and M are the length of the strings T and S respectively.
Auxiliary Space: O(D) where D is the size of the array replace[][].

Related Articles:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!