Open In App

Java Program To Check If A String Is Substring Of Another

Write a java program for a given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1.

Examples : 



Input: s1 = “for”, s2 = “geeksforgeeks”
Output: 5
Explanation: String “for” is present as a substring of s2.

Input: s1 = “practice”, s2 = “geeksforgeeks”
Output: -1.
Explanation: There is no occurrence of “practice” in”geeksforgeeks”



Naive Approach:

Run a loop from start to end and for every index in the given string check whether the sub-string can be formed from that index. This can be done by running a nested loop traversing the given string and in that loop running another loop checking for sub-strings starting from every index.

Follow the steps below to implement the idea:

Below is the Implementation of the above idea.

Java

// Java program to check if a string is
// substring of other.
class GFG {

    // Returns true if s1 is substring of s2
    static int isSubstring(String s1, String s2)
    {
        int M = s1.length();
        int N = s2.length();

        /* A loop to slide pat[] one by one */
        for (int i = 0; i <= N - M; i++) {
            int j;

            /* For current index i, check for
            pattern match */
            for (j = 0; j < M; j++)
                if (s2.charAt(i + j) != s1.charAt(j))
                    break;

            if (j == M)
                return i;
        }

        return -1;
    }

    /* Driver code */
    public static void main(String args[])
    {
        String s1 = "for";
        String s2 = "geeksforgeeks";

        int res = isSubstring(s1, s2);

        if (res == -1)
            System.out.println("Not present");
        else
            System.out.println("Present at index " + res);
    }
}

// This code is contributed by JaideepPyne.

Output
Present at index 5

Complexity Analysis: 

Java Program To Check If A String Is Substring Of Another using Java Library:

The indexOf method in Java is used for finding the starting index of a substring in a given string.

Below is the Implementation of above approach.

Java

// Java program to implement the approach

class GFG {
    // function to get the index of s2 in s1
    static int isSubstring(String s1, String s2)
    {
        // using indexOf method to check if s1 is
        // a substring of s2
        return s2.indexOf(s1);
    }
    public static void main(String[] args)
    {
        String s1 = "for";
        String s2 = "geeksforgeeks";

        // Function Call
        int res = isSubstring(s1, s2);
        if (res == -1)
            System.out.println("Not present");
        else
            System.out.println("Present at index " + res);
    }
}

// this code is contributed by phasing17

Output
Present at index 5

Time Complexity: O(N) , where N is the length of the longer string s2
Auxiliary space: O(1)

Please refer complete article on

Check if a string is substring of another

for more details!

Article Tags :