Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // attention: pay attention the the end index limit on all arrays (including strings)
- class Solution {
- public int strStr(String haystack, String needle) {
- if (null == haystack) return -1;
- if (null == needle) return -1;
- if (needle.isEmpty()) return 0; // if empty string, return 0
- for (int i = 0; i <= haystack.length() - needle.length(); i++) {
- boolean isFound = true;
- for (int j = 0; j < needle.length(); j++) {
- if (haystack.charAt(i+j) != needle.charAt(j)) {
- isFound = false;
- break;
- }
- }
- if (isFound) {
- return i;
- }
- }
- return -1;
- }
- };
Add Comment
Please, Sign In to add comment