import
java.util.*;
public
class
GFG {
public
static
int
numOccurrences(String[] arr,
int
n, String str,
int
L,
int
R) {
Map<String, List<Integer>> stringIndices =
new
HashMap<>();
for
(
int
i =
0
; i < n; i++) {
String currentString = arr[i];
List<Integer> indices = stringIndices.get(currentString);
if
(indices ==
null
) {
indices =
new
ArrayList<>();
indices.add(i +
1
);
stringIndices.put(currentString, indices);
}
else
{
indices.add(i +
1
);
}
}
List<Integer> indices = stringIndices.get(str);
if
(indices ==
null
) {
return
0
;
}
Collections.sort(indices);
int
upperBound = upperBound(indices, R);
int
lowerBound = upperBound(indices, L -
1
);
return
upperBound - lowerBound;
}
private
static
int
upperBound(List<Integer> list,
int
value) {
int
left =
0
;
int
right = list.size() -
1
;
while
(left <= right) {
int
mid = (left + right) /
2
;
if
(list.get(mid) <= value) {
left = mid +
1
;
}
else
{
right = mid -
1
;
}
}
return
left;
}
public
static
void
main(String[] args) {
String[] arr = {
"abc"
,
"abcabc"
,
"abc"
};
int
n = arr.length;
int
L =
1
;
int
R =
3
;
String str =
"abc"
;
System.out.println(numOccurrences(arr, n, str, L, R));
}
}