#K4371. String Manipulation Operations
String Manipulation Operations
String Manipulation Operations
This problem requires you to perform various operations on a string according to given commands. You must implement three functions:
- insert_character(s, pos, ch): Inserts the character ch into the string s at position pos. The position is 0-indexed.
- delete_character(s, pos): Deletes the character in the string s located at position pos.
- count_distinct_characters(s): Returns the number of distinct characters in the string s using \( \LaTeX \) notation for formulas when needed (e.g., \( |\{\text{characters in } s\}| \)).
You will be given an initial string and then a number of operations. The operations follow one of these formats:
I pos ch
: Insert characterch
at positionpos
.D pos
: Delete the character at positionpos
.C
: Count distinct characters in the current string and output the result.
Your program should process the commands in order and for each C
command, output the number of distinct characters on a new line.
inputFormat
The input is read from stdin and has the following format:
- The first line contains the initial string s.
- The second line contains an integer Q, the number of operations.
- The following Q lines each contain one operation, which can be one of the following:
I pos ch
— Insert characterch
at indexpos
(0-indexed).D pos
— Delete the character at indexpos
.C
— Output the count of distinct characters in the string.
outputFormat
For each C
command encountered in the input, output a single line containing the number of distinct characters in the string at that point. The output should be written to stdout.
abcde
5
I 3 z
C
D 0
C
I 0 x
6
5
</p>