📌  相关文章
📜  检查子串 S1 是否出现在给定句子中任何出现子串 S2 之后

📅  最后修改于: 2022-05-13 01:57:07.593000             🧑  作者: Mango

检查子串 S1 是否出现在给定句子中任何出现子串 S2 之后

给定字符串S1S2S ,任务是检查S的每个与S1相同的子是否在其之前有另一个与S2相同的S子串。假定S1始终作为字符串S中的子字符串存在。

例子:

方法:该方法是检查哪个子字符串首先出现。如果首先出现子字符串 S2,则返回 true。如果 S1 首先发生,则返回 false。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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 = "sxygeeksgcodetecode";
    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 = "sxygeeksgcodetecode";
    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 = "sxygeeksgcodetecode"
 
    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 = "sxygeeksgcodetecode";
    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



输出
True

时间复杂度: O(N * K)
辅助空间: O(1)