String Question 4

Home / Java Practice Question / String Question 4

String Question 4


Question 4. Write a Program to find a first occurrence of of needle in string, and return -1 if it is not a part of string.

Program-



class Solution {

    public int strStr(String haystack, String needle) {

        int hayLen = haystack.length();

        int needleLen = needle.length();

        if (hayLen < needleLen) {

            return -1;

        }

        for (int i = 0; i <= hayLen - needleLen; i++) {

            if (haystack.charAt(i) == needle.charAt(0)) {

                boolean found = true;

                for (int j = 1; j < needleLen; j++) {

                    if (haystack.charAt(i + j) != needle.charAt(j)) {

                        found = false;

                        break;

                    }

                }

                if (found) {

                    return i;

                }

            }

        }

        return -1;

    }

}