1

I need a proper data structure for a collection of strings. Users may query for the current number of strings in the collection that have a certain prefix or suffix, and subsequently remove these strings from the collection if they choose to. Users may also insert one string; if a string is inserted more than once, it should be returned multiple times.

The time complexity of query operations should be O(m + log n), where m is the expected size of the result of the operation.

I am thinking of using a Trie but we will have to use 2 tries one for the suffix and another for prefix, the problem with the approach is we won't be able to apply remove() in required time in that case.

0

1 Answer 1

1

Data structure that you need is called trie, or prefix tree. You can combine two prefix trees, one containing original strings and another, containing inverted strings. Searching must be performed on one of the prefix trees, depends on what you need - suffix or prefix. With this data structure you can search not only by suffix and prefix, you can find strings, that contains arbitrary substring.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.