Input: s ="isawsquirrelnearmysquirrelhouseohmy", a ="my", b ="squirrel", k =15 Output:[16,33] Explanation: There are 2 beautiful indices:[16,33].- The index 16is beautiful as s[16..17]=="my" and there exists an index 4with s[4..11]=="squirrel" and |16-4|<=15.- The index 33is beautiful as s[33..34]=="my" and there exists an index 18with s[18..25]=="squirrel" and |33-18|<=15. Thus we return[16,33] as the result.
Input: s ="abcd", a ="a", b ="a", k =4 Output:[0] Explanation: There is1 beautiful index:[0].- The index 0is beautiful as s[0..0]=="a" and there exists an index 0with s[0..0]=="a" and |0-0|<=4. Thus we return[0] as the result.
We need to find all indices where substring a occurs in s, and for each such index, check if there is any occurrence of substring b within distance k. Since s can be very large, we use direct string matching to find all start indices of a and b, then use two pointers to efficiently check the distance condition.
classSolution {
public: vector<int> beautifulIndices(string s, string a, string b, int k) {
vector<int> apos, bpos, ans;
for (int i =0; i + a.size() <= s.size(); ++i)
if (s.substr(i, a.size()) == a) apos.push_back(i);
for (int i =0; i + b.size() <= s.size(); ++i)
if (s.substr(i, b.size()) == b) bpos.push_back(i);
int j =0;
for (int i : apos) {
while (j < bpos.size() && bpos[j] < i - k) ++j;
int jj = j;
while (jj < bpos.size() && bpos[jj] <= i + k) {
ans.push_back(i);
break;
}
}
return ans;
}
};
classSolution {
public List<Integer>beautifulIndices(String s, String a, String b, int k) {
List<Integer> apos =new ArrayList<>(), bpos =new ArrayList<>(), ans =new ArrayList<>();
for (int i = 0; i + a.length() <= s.length(); i++)
if (s.substring(i, i + a.length()).equals(a)) apos.add(i);
for (int i = 0; i + b.length() <= s.length(); i++)
if (s.substring(i, i + b.length()).equals(b)) bpos.add(i);
int j = 0;
for (int i : apos) {
while (j < bpos.size() && bpos.get(j) < i - k) j++;
int jj = j;
while (jj < bpos.size() && bpos.get(jj) <= i + k) {
ans.add(i);
break;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funbeautifulIndices(s: String, a: String, b: String, k: Int): List<Int> {
val apos = mutableListOf<Int>()
val bpos = mutableListOf<Int>()
for (i in0..s.length - a.length) if (s.substring(i, i + a.length) == a) apos.add(i)
for (i in0..s.length - b.length) if (s.substring(i, i + b.length) == b) bpos.add(i)
val ans = mutableListOf<Int>()
var j = 0for (i in apos) {
while (j < bpos.size && bpos[j] < i - k) j++var jj = j
while (jj < bpos.size && bpos[jj] <= i + k) {
ans.add(i)
break }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defbeautifulIndices(self, s: str, a: str, b: str, k: int) -> list[int]:
apos = [i for i in range(len(s) - len(a) +1) if s[i:i+len(a)] == a]
bpos = [i for i in range(len(s) - len(b) +1) if s[i:i+len(b)] == b]
ans = []
j =0for i in apos:
while j < len(bpos) and bpos[j] < i - k:
j +=1 jj = j
while jj < len(bpos) and bpos[jj] <= i + k:
ans.append(i)
breakreturn ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnbeautiful_indices(s: String, a: String, b: String, k: i32) -> Vec<i32> {
let apos: Vec<_>= (0..=s.len()-a.len()).filter(|&i|&s[i..i+a.len()] == a).collect();
let bpos: Vec<_>= (0..=s.len()-b.len()).filter(|&i|&s[i..i+b.len()] == b).collect();
letmut ans =vec![];
letmut j =0;
for&i in&apos {
while j < bpos.len() && bpos[j] < i asi32- k { j +=1; }
letmut jj = j;
while jj < bpos.len() && bpos[jj] <= i asi32+ k {
ans.push(i asi32);
break;
}
}
ans
}
}