Sunday, October 6, 2024
Google search engine
HomeData Modelling & AICheck if substring S1 appear after any occurrence of substring S2 in...

Check if substring S1 appear after any occurrence of substring S2 in given sentence

Given strings S1, S2 and S, the task is to check if every substring of S which is same as S1 has another substring of S same as S2 before it. It is given that S1 is always present as a substring in the string S.

Examples:

Input: S1 = “code”, S2 = “geek”, S = “sxyneveropengcodetecode”
Output: True
Explanation: Substring S2 is present before both the occurrence of S1.
“sxygeeksgcodetecode

Input: S1  = “code”, S2 = “my”, “sxycodesforneveropenvhgh”
Output: False

 

Approach: The approach is to check which substring occurs first. If substring S2 occurs first return true. If S1 occurs first return false.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if S2 is present
// before all S1 in string S
bool chekfavstring(string& S, string& S1,
                   string& S2)
{
    bool cod = false;
    int n = S.size();
    int n1 = S1.size(), n2 = S2.size();
    for (int i = 0; i <= n - n2; i++) {
        string str;
        for (int k = i; k < i + n2; k++) {
            str.push_back(S[k]);
        }
        if (str == S2) {
            return true;
        }
        if (str == S1) {
            return false;
        }
    }
    return true;
}
 
// Driver code
int main()
{
    string S = "sxyneveropengcodetecode";
    string S1 = "code", S2 = "geek";
    chekfavstring(S, S1, S2) ? cout << "True"
                             : cout << "False";
    return 0;
}


Java




// Java program to implement
// the above approach
class GFG {
 
  // Function to check if S2 is present
  // before all S1 in string S
  static boolean chekfavstring(String S, String S1,
                               String S2)
  {
    int n = S.length();
    int n1 = S1.length(), n2 = S2.length();
    for (int i = 0; i <= n - n2; i++) {
      String str = "";
      for (int k = i; k < i + n2; k++) {
        str += S.charAt(k);
      }
      if (str == S2) {
        return true;
      }
      if (str == S1) {
        return false;
      }
    }
    return true;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String S = "sxyneveropengcodetecode";
    String S1 = "code", S2 = "geek";
 
    if (chekfavstring(S, S1, S2)) {
      System.out.print("True");
    }
    else {
      System.out.print("False");
    }
  }
}
 
// This code is contributed by ukasp.


Python3




# python3 program for the above approach
 
# Function to check if S2 is present
# before all S1 in string S
def chekfavstring(S, S1, S2):
 
    cod = False
    n = len(S)
    n1 = len(S1)
    n2 = len(S2)
 
    for i in range(0, n - n2 + 1):
        str = ""
 
        for k in range(i, i + n2):
            str += S[k]
 
        if (str == S2):
            return True
 
        if (str == S1):
            return False
 
    return True
 
# Driver code
if __name__ == "__main__":
 
    S = "sxyneveropengcodetecode"
 
    S1 = "code"
    S2 = "geek"
 
    print("True") if chekfavstring(S, S1, S2) else print("False")
 
    # This code is contributed by rakeshsahni


C#




// C# program to implement
// the above approach
using System;
class GFG
{
 
// Function to check if S2 is present
// before all S1 in string S
static bool chekfavstring(string S, string S1,
                   string S2)
{
    int n = S.Length;
    int n1 = S1.Length, n2 = S2.Length;
    for (int i = 0; i <= n - n2; i++) {
        string str = "";
        for (int k = i; k < i + n2; k++) {
            str += S[k];
        }
        if (str == S2) {
            return true;
        }
        if (str == S1) {
            return false;
        }
    }
    return true;
}
 
 
// Driver Code
public static void Main()
{
    string S = "sxyneveropengcodetecode";
    string S1 = "code", S2 = "geek";
     
    if(chekfavstring(S, S1, S2)) {
        Console.Write("True");
    }
    else {
        Console.Write("False");
    }
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
       // JavaScript code for the above approach
 
 
       // Function to check if S2 is present
       // before all S1 in string S
       function chekfavstring(S, S1,
           S2) {
           let cod = false;
           let n = S.length;
           let n1 = S1.length, n2 = S2.length;
           for (let i = 0; i <= n - n2; i++) {
               let str = "";
               for (let k = i; k < i + n2; k++) {
                   str += (S[k]);
               }
               if (str == S2) {
                   return true;
               }
               if (str == S1) {
                   return false;
               }
           }
           return true;
       }
 
       // Driver code
 
       let S = "sxyneveropengcodetecode";
       let S1 = "code", S2 = "geek";
       chekfavstring(S, S1, S2) ? document.write("True")
           : document.write("False")
 
 // This code is contributed by Potta Lokesh
   </script>


 
 

Output

True

 

Time Complexity: O(N * K)
Auxiliary Space: O(1) 

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments