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.
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.
1 0 1
This article is contributed by Bhupinder. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C Program for Binary Search (Recursive and Iterative)
- Floor value Kth root of a number using Recursive Binary Search
- Recursive program to linearly search an element in a given array
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Longest substring with K unique characters using Binary Search
- Longest substring consisting of vowels using Binary Search
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Sublist Search (Search a linked list in another list)
- Linear Search vs Binary Search
- Repeatedly search an element by doubling it after every successful search
- Meta Binary Search | One-Sided Binary Search
- Best First Search (Informed Search)
- Recursive function to check if a string is palindrome
- Function to copy string (Iterative and Recursive)
- Program to count vowels in a string (Iterative and Recursive)
- Recursive Programs to find Minimum and Maximum elements of array