Given a text txt[] and a pattern pat[], write a recursive function “contains(char pat[], char txt[])” that returns true if pat[] is present in txt[], otherwise false. Examples:
1) Input: txt[] = "THIS IS A TEST TEXT"
pat[] = "TEST"
Output: true
2) Input: txt[] = "geeksforgeeks"
pat[] = "quiz"
Output: false;
We strongly recommend to minimize the browser and try this yourself first. Below is recursive algorithm.
contains(tex[], pat[])
1) If the current character is the last character of the text, but pat
has more characters, return false.
2) Else If the current character is the last character of the pattern,
then return true
3) Else If current characters of pat and text match, then
return contains(text + 1, pat + 1);
4) Else If current characters of pat and text don't match
return contains(text + 1, pat);
Below is the implementation of the above algorithm.
C++
#include<iostream>
using namespace std;
bool exactMatch( char *text, char *pat)
{
if (*text == '\0' && *pat != '\0' )
return false ;
if (*pat == '\0' )
return true ;
if (*text == *pat)
return exactMatch(text + 1, pat + 1);
return false ;
}
bool contains( char *text, char *pat)
{
if (*text == '\0' )
return false ;
if (*text == *pat)
if (exactMatch(text, pat))
return 1;
else
return contains(text + 1, pat);
return contains(text + 1, pat);
}
int main()
{
cout << contains( "geeksforgeeks" , "geeks" ) << endl;
cout << contains( "geeksforgeeks" , "geeksquiz" ) << endl;
cout << contains( "geeksquizgeeks" , "quiz" ) << endl;
return 0;
}
|
Java
class GFG {
static int exactMatch(String text, String pat, int text_index, int pat_index) {
if (text_index == text.length() && pat_index != pat.length())
return 0 ;
if (pat_index == pat.length())
return 1 ;
if (text.charAt(text_index) == pat.charAt(pat_index))
return exactMatch(text, pat, text_index + 1 , pat_index + 1 );
return 0 ;
}
static int contains(String text, String pat, int text_index, int pat_index) {
if (text_index == text.length())
return 0 ;
if (text.charAt(text_index) == pat.charAt(pat_index)) {
if (exactMatch(text, pat, text_index, pat_index) == 1 )
return 1 ;
else
return contains(text, pat, text_index + 1 , pat_index);
}
return contains(text, pat, text_index + 1 , pat_index);
}
public static void main(String args[]) {
System.out.println(contains( "geeksforgeeks" , "geeks" , 0 , 0 ));
System.out.println(contains( "geeksforgeeks" , "geeksquiz" , 0 , 0 ));
System.out.println(contains( "geeksquizgeeks" , "quiz" , 0 , 0 ));
}
}
|
Python3
def exactMatch(text, pat, text_index, pat_index):
if text_index = = len (text) and pat_index ! = len (pat):
return 0
if pat_index = = len (pat):
return 1
if text[text_index] = = pat[pat_index]:
return exactMatch(text, pat, text_index + 1 , pat_index + 1 )
return 0
def contains(text, pat, text_index, pat_index):
if text_index = = len (text):
return 0
if text[text_index] = = pat[pat_index]:
if exactMatch(text, pat, text_index, pat_index):
return 1
else :
return contains(text, pat, text_index + 1 , pat_index)
return contains(text , pat, text_index + 1 , pat_index)
print (contains( "geeksforgeeks" , "geeks" , 0 , 0 ))
print (contains( "geeksforgeeks" , "geeksquiz" , 0 , 0 ))
print (contains( "geeksquizgeeks" , "quiz" , 0 , 0 ))
|
C#
using System;
public class GFG
{
public static int exactMatch(String text, String pat, int text_index, int pat_index)
{
if (text_index == text.Length && pat_index != pat.Length)
{
return 0;
}
if (pat_index == pat.Length)
{
return 1;
}
if (text[text_index] == pat[pat_index])
{
return GFG.exactMatch(text, pat, text_index + 1, pat_index + 1);
}
return 0;
}
public static int contains(String text, String pat, int text_index, int pat_index)
{
if (text_index == text.Length)
{
return 0;
}
if (text[text_index] == pat[pat_index])
{
if (GFG.exactMatch(text, pat, text_index, pat_index) == 1)
{
return 1;
}
else
{
return GFG.contains(text, pat, text_index + 1, pat_index);
}
}
return GFG.contains(text, pat, text_index + 1, pat_index);
}
public static void Main(String[] args)
{
Console.WriteLine(GFG.contains( "geeksforgeeks" , "geeks" , 0, 0));
Console.WriteLine(GFG.contains( "geeksforgeeks" , "geeksquiz" , 0, 0));
Console.WriteLine(GFG.contains( "geeksquizgeeks" , "quiz" , 0, 0));
}
}
|
Javascript
<script>
function exactMatch(text, pat, text_index, pat_index){
if (text_index == text.length && pat_index != pat.length)
return 0
if (pat_index == pat.length)
return 1
if (text[text_index] == pat[pat_index])
return exactMatch(text, pat, text_index+1, pat_index+1)
return 0
}
function contains(text, pat, text_index, pat_index){
if (text_index == text.length)
return 0
if (text[text_index] == pat[pat_index]){
if (exactMatch(text, pat, text_index, pat_index))
return 1
else
return contains(text, pat, text_index+1, pat_index)
}
return contains(text , pat, text_index+1, pat_index)
}
document.write(contains( "geeksforgeeks" , "geeks" , 0, 0), "</br>" )
document.write(contains( "geeksforgeeks" , "geeksquiz" , 0, 0), "</br>" )
document.write(contains( "geeksquizgeeks" , "quiz" , 0, 0), "</br>" )
</script>
|
Output:
1
0
1
Time Complexity : O(n*m)
Auxiliary Space : O(n+m)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Java
public class Main {
public static boolean str_search(String st, String text,
int k)
{
int n = st.length();
int m = text.length();
if (k
< n - m + 1 ) {
if (st.substring(k, k + m).equals(
text)) {
return true ;
}
else {
return str_search(
st, text,
k + 1 );
}
}
else {
return false ;
}
}
public static void main(String[] args)
{
String t = "this is a test text" ;
System.out.println(str_search(
t, "test" ,
0 ));
}
}
|
Python3
def str_search(st, text, k):
n = len (st)
m = len (text)
if k < n - m + 1 :
if st[k:k + m] = = text:
return True
else :
return str_search(st, text, k + 1 )
else :
return False
t = 'this is a test text'
print (str_search(t, 'test' , 0 ))
|
C#
using System;
class MainClass {
public static bool StrSearch( string st, string text, int k) {
int n = st.Length;
int m = text.Length;
if (k < n - m + 1) {
if (st.Substring(k, m).Equals(text)) {
return true ;
} else {
return StrSearch(st, text, k + 1);
}
} else {
return false ;
}
}
static void Main() {
string t = "this is a test text" ;
Console.WriteLine(StrSearch(t, "test" , 0));
}
}
|
Javascript
function str_search(st, text, k) {
let n = st.length;
let m = text.length;
if (k < n - m + 1) {
if (st.substring(k, k + m) === text) {
return true ;
} else {
return str_search(st, text, k + 1);
}
} else {
return false ;
}
}
let t = 'this is a test text' ;
console.log(str_search(t, 'test' , 0));
|
C++14
#include <iostream>
#include <string>
using namespace std;
bool str_search(string st, string text, int k)
{
int n = st.length();
int m = text.length();
if (k < n - m + 1)
{
if (st.substr(k, m) == text)
{
return true ;
}
else
{
return str_search(st, text, k + 1);
}
}
else
{
return false ;
}
}
int main()
{
string t = "this is a test text" ;
if (str_search(t, "test" , 0))
cout<< "true" << endl;
else
cout<< "false" <<endl;
return 0;
}
|
Time Complexity : O((n-m+1)*m), where n is the length of the string to search in and m is the length of the string to search for.
Auxiliary Space : O(m), because the only additional memory used is the substring of length m that is created for each recursive call.
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 geeks!