#C335. Phone Directory Management
Phone Directory Management
Phone Directory Management
You are required to implement a phone directory that supports the following operations in a case-insensitive manner:
- add: Add a new contact. The contact name is considered the same regardless of letter case. If the contact (ignoring case) already exists, the operation fails.
- delete: Delete an existing contact. If the contact does not exist (case-insensitive), the operation fails.
- search: Search for all contacts that have a given prefix (case-insensitive). The matching contacts should be returned in lexicographical order (ignoring case).
- list: List all contacts in the phone directory in ascending lexicographical order (ignoring case).
All input processing should be done from stdin and all outputs printed to stdout. When adding or deleting a contact, output True
or False
on a new line. For the search
and list
operations, output the matching contact names separated by a single space on one line. If there is no contact to show for the search
operation, output an empty line.
Notes:
- Contact names may contain spaces and extra whitespace should be trimmed.
- The comparisons should be made in a case-insensitive way. Internally, you might use the lowercase version of strings.
The correctness of your solution will be judged on multiple test cases. Make sure to handle edge cases such as empty names or names with only spaces.
inputFormat
The input starts with an integer N (1 ≤ N ≤ 105), which denotes the number of operations. Each of the following N lines contains an operation command.
Each command is one of the following four types:
- add <contact_name> — Add a new contact. The contact name may contain spaces.
- delete <contact_name> — Delete an existing contact.
- search <prefix> — Search for all contacts starting with the given prefix.
- list — List all contacts currently in the directory.
Note: For commands that include a contact name or prefix, the command keyword and the argument are separated by at least one space. Extra leading or trailing spaces should be ignored.
outputFormat
For each operation, output a line as follows:
- For add and delete operations, output
True
if the operation succeeds orFalse
if it fails. - For the search operation, output the matching contact names separated by a single space. If no contacts match, output an empty line.
- For the list operation, output all contact names separated by a single space. If the directory is empty, output an empty line.
6
add Alice Wonderland
add alice wonderland
add Bob Builder
list
search al
delete Alice Wonderland
True
False
True
Alice Wonderland Bob Builder
Alice Wonderland
True
</p>