forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_3006.java
31 lines (29 loc) · 1.06 KB
/
_3006.java
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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _3006 {
public static class Solution1 {
public List<Integer> beautifulIndices(String s, String a, String b, int k) {
List<Integer> aIndices = new ArrayList<>();
List<Integer> bIndices = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if ((i + a.length()) <= s.length() && s.substring(i, i + a.length()).equals(a)) {
aIndices.add(i);
}
if ((i + b.length()) <= s.length() && s.substring(i, i + b.length()).equals(b)) {
bIndices.add(i);
}
}
List<Integer> result = new ArrayList<>();
for (int aIndex : aIndices) {
for (int bIndex : bIndices) {
if (Math.abs(aIndex - bIndex) <= k) {
result.add(aIndex);
break;
}
}
}
return result;
}
}
}