Open In App

Check if a string is substring of another

Last Updated : 16 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given two strings txt and pat, the task is to find if pat is a substring of txt. If yes, return the index of the first occurrence, else return -1.

Examples : 

Input: txt = “geeksforgeeks”, pat = “eks”
Output: 2
Explanation: String “eks” is present at index 2 and 9, so 2 is the smallest index.

Input: txt = “geeksforgeeks”, pat = “xyz”
Output: -1.
Explanation: There is no occurrence of “xyz” in “geeksforgeeks”

Using nested loops – O(m*n) Time and O(1) Space

The basic idea is to iterate through a loop in the string txt and for every index in the string txt, check whether we can match pat starting from this index. This can be done by running a nested loop for traversing the string pat and checking for each character of pat matches with txt or not. If all character of pat matches then return the starting index of such substring.

C++
// C++ program to check if a string is substring of other
// using nested loops

#include <iostream>
using namespace std;

// Function to find if pat is a substring of txt
int findSubstring(string &txt, string &pat) {
    int n = txt.length();
    int m = pat.length();

    // Iterate through txt
    for (int i = 0; i <= n - m; i++) {

        // Check for substring match
        int j;
        for (j = 0; j < m; j++) {

            // Mismatch found
            if (txt[i + j] != pat[j]) {
                break;
            }
        }

        // If we completed the inner loop, we found a match
        if (j == m) {

            // Return starting index
            return i;
        }
    }
    
    // No match found
    return -1;
}

int main() {
    string txt = "geeksforgeeks";
    string pat = "eks";
    cout << findSubstring(txt, pat);

    return 0;
}
C Java Python C# JavaScript

Output
2

Time complexity: O(m * n) where m and n are lengths of txt and pat respectively. 
Auxiliary Space: O(1), as no extra space is required.

Using Pattern Searching Algorithms

There are several Pattern Searching Algorithms like KMP Algorithm, Rabin-Karp Algorithm, Aho-Corasick Algorithm, etc. Please refer to Introduction to Pattern Searching to learn more about them.

Using in-built library functions

This approach uses a built-in function to quickly check if pattern is part of text or not. This makes the process simple and efficient.

C++
// C++ program to check if a string is substring of other
// using in-built functions

#include <iostream>
using namespace std;

int main() {
    string txt = "geeksforgeeks";
    string pat = "eks";
    
    // If pat is found, returns the index of first
    // occurrence of pat. Otherwise, returns a special
    // constant value std::string::npos
    size_t idx = txt.find(pat);
  
    if(idx != string::npos)
        cout << idx;
    else
        cout << -1;

    return 0;
}
C Java Python C# JavaScript

Output
2

Note: The time complexity of in-built functions can differ across different languages.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg