
-Code
class Solution {
public int strStr(String haystack, String needle) {
int haystackLen= haystack.length();
int needleLen = needle.length();
for (int i = 0; i <= haystackLen - needleLen; i++) {
// 현제 index에서 needle의 길이만큼 자르기
String now = haystack.substring(i, i + needleLen);
// 일치하면 반환
if(now.equals(needle)) return i;
}
// 일치하는게 없으면 -1 반환
return -1;
}
}'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [LeetCode/Java] 58. Length of Last Word (0) | 2025.11.20 |
|---|---|
| [LeetCode/Java] 35. Search Insert Position (0) | 2025.11.20 |
| [백준/Java] 2252번 줄 세우기 (0) | 2025.11.20 |
| [백준/Java] 34750번 추석은 언제나 좋아 (0) | 2025.11.20 |
| [LeetCode/Java] 21. Merge Two Sorted Lists (0) | 2025.11.19 |