forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
40 lines (37 loc) · 1.07 KB
/
Solution.java
File metadata and controls
40 lines (37 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package g3401_3500.s3407_substring_matching_pattern;
// #Easy #String #String_Matching #2025_01_06_Time_1_ms_(100.00%)_Space_42.63_MB_(100.00%)
public class Solution {
public boolean hasMatch(String s, String p) {
int index = -1;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '*') {
index = i;
break;
}
}
int num1 = fun(s, p.substring(0, index));
if (num1 == -1) {
return false;
}
int num2 = fun(s.substring(num1), p.substring(index + 1));
return num2 != -1;
}
private int fun(String s, String k) {
int n = s.length();
int m = k.length();
int j;
for (int i = 0; i <= n - m; i++) {
for (j = 0; j < m; j++) {
char ch1 = s.charAt(j + i);
char ch2 = k.charAt(j);
if (ch1 != ch2) {
break;
}
}
if (j == m) {
return i + j;
}
}
return -1;
}
}