Method
strStr
(String haystack, String needle)
Source from the content-addressed store, hash-verified
| 10 | */ |
| 11 | public class Solution { |
| 12 | public int strStr(String haystack, String needle) { |
| 13 | int l1 = haystack.length(), l2 = needle.length(); |
| 14 | if (l1 < l2) return -1; |
| 15 | for (int i = 0; ; i++) { |
| 16 | if (i + l2 > l1) return -1; |
| 17 | for (int j = 0; ; j++) { |
| 18 | if (j == l2) return i; |
| 19 | if (haystack.charAt(i + j) != needle.charAt(j)) break; |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | public static void main(String[] args) { |
| 25 | Solution solution = new Solution(); |
Tested by
no test coverage detected