class Solution {
public int strStr(String haystack, String needle) {
int result = -1;
if (haystack.length() < needle.length()) {
return result;
}
for(int i = 0; i <= haystack.length() - needle.length(); i++) {
if (needle.charAt(0) == haystack.charAt(i)) {
boolean same = true;
for(int j = 0; j < needle.length(); j++) {
if (needle.charAt(j) != haystack.charAt(i + j)) {
same = false;
break;
}
}
if (same) {
result = i;
break;
}
}
}
return result;
}
}
'코딩테스트' 카테고리의 다른 글
[leetcode] 80. Remove Duplicates from Sorted Array II (0) | 2024.08.03 |
---|---|
[leetcode] 26. Remove Duplicates from Sorted Array (0) | 2024.08.03 |
[leetcode] 14. Longest Common Prefix (0) | 2024.08.01 |
[leetcode] 189. Rotate Array (0) | 2024.07.27 |
[leetcode] 12. Integer to Roman (0) | 2024.07.19 |