Search

Imran Pollob
Imran Pollob
  • Home
  • Contact
  • Blog++
    Blog Data Structures & Algorithms Leetcode solution Hackerrank solution Codesignal solution
  • Light Dark
  • LEETCODE SOLUTIONS
  • 1920 - Build Array from Permutation
  • 1929 - Concatenation of Array
  • 1480 - Running Sum of 1d Array
  • 28 - Implement strStr()
  • 7 - Reverse Integer
  • 9 - Palindrome Number
  • 283 - Move Zeros
  • 485 - Max Consecutive Ones
  • Contents
  1. Home
  2. LEETCODE SOLUTIONS
  3. 28 - Implement strStr()

Leetcode 28 - Implement strStr() solution

Problem link

Solution:

class Solution {
    public int strStr(String haystack, String needle) {
        int nl = needle.length();

        for(int i = 0; i <= haystack.length() - nl; i = i+1) {
            if (haystack.substring(i, i + nl).equals(needle)) {
                return i;
            }
        }
        
        return -1;
    }
}
Previous
Leetcode 1480 - Running Sum of 1d Array solution
Next
Leetcode 7 - Reverse Integer solution

Last updated on Dec 20, 2021

© 2022 Imran Pollob

Published with Wowchemy — the free, open source website builder that empowers creators.

Cite
Copy Download