import
java.util.*;
import
java.util.stream.*;
public
class
Main {
public
static
void
main(String[] args) {
String S =
"abcdbbd"
;
int
N = S.length();
Map<Integer, TreeSet<Integer>> hMap =
new
HashMap<>();
insert(hMap, S, N);
query2(hMap,
3
,
6
);
query1(S, hMap,
5
,
'z'
);
query2(hMap,
1
,
1
);
query1(S, hMap,
4
,
'a'
);
query1(S, hMap,
7
,
'd'
);
query2(hMap,
1
,
7
);
}
public
static
void
insert(Map<Integer, TreeSet<Integer>> hMap, String S,
int
N) {
for
(
int
i =
0
; i < N; i++) {
int
c = S.charAt(i) -
'a'
;
if
(!hMap.containsKey(c)) {
hMap.put(c,
new
TreeSet<>());
}
hMap.get(c).add(i);
}
}
public
static
void
query1(String S, Map<Integer, TreeSet<Integer>> hMap,
int
pos,
char
c) {
pos--;
int
previous = S.charAt(pos) -
'a'
;
int
current = c -
'a'
;
char
[] chars = S.toCharArray();
chars[pos] = c;
S = String.valueOf(chars);
hMap.get(previous).remove(pos);
if
(!hMap.containsKey(current)) {
hMap.put(current,
new
TreeSet<>());
}
hMap.get(current).add(pos);
}
public
static
void
query2(Map<Integer, TreeSet<Integer>> hMap,
int
L,
int
R) {
int
count =
0
;
L--;
R--;
for
(
int
i =
0
; i <
26
; i++) {
if
(hMap.containsKey(i)) {
Integer temp = hMap.get(i).floor(R);
if
(temp !=
null
&& temp >= L) {
count++;
}
}
}
System.out.println(count);
}
}