#C10786. String Insertion and Uniqueness Check
String Insertion and Uniqueness Check
String Insertion and Uniqueness Check
You are given a list of operations to be executed on an initially empty string database. There are two possible operations:
- INSERT s: Inserts the string s into the database. If the string already exists, output "String already exists"; otherwise, output "Inserted successfully".
- CHECK s: Checks if the string s exists in the database. If it exists, output "String found"; otherwise, output "String not found".
The operations are given one per line and processing stops when the command "END" is encountered. Each output should be printed on a new line in the same order in which operations are processed.
Note: The command END is not processed.
The logic behind the solution is to maintain a set (or similar data structure) which stores the inserted strings. Inserting or checking a string is then performed with an average time complexity of \(O(1)\) per operation.
inputFormat
The input will consist of multiple lines. Each line contains an operation in one of the following formats:
INSERT s
CHECK s
The input terminates with a line containing only END
, which should not be processed.
outputFormat
For each operation (except the terminating "END"), print the corresponding result on a new line. The possible outputs are:
- Inserted successfully
- String already exists
- String found
- String not found
INSERT hello
INSERT world
CHECK hello
CHECK WORLD
INSERT hello
END
Inserted successfully
Inserted successfully
String found
String not found
String already exists
</p>