Make your first Pull Request for Hacktoberfest 2024! Don’t forget to spread the love, and if you find it helpful, consider giving us a ⭐️. This repository includes useful algorithms and code to help beginners, featuring multiple languages and ideas. You’ll also find some beginner-friendly mini projects to get you started!
Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the AllOne class:
AllOne() Initializes the object of the data structure.
inc(String key) Increments the count of the string key by 1. If key does not exist in the data structure, insert it with count 1.
dec(String key) Decrements the count of the string key by 1. If the count of key is 0 after the decrement, remove it from the data structure. It is guaranteed that key exists in the data structure before the decrement.
getMaxKey() Returns one of the keys with the maximal count. If no element exists, return an empty string "".
getMinKey() Returns one of the keys with the minimum count. If no element exists, return an empty string "".
Note that each function must run in O(1) average time complexity.
All O`one Data Structure
Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the
AllOne
class:AllOne()
Initializes the object of the data structure.inc(String key)
Increments the count of the stringkey
by1
. Ifkey
does not exist in the data structure, insert it with count1
.dec(String key)
Decrements the count of the stringkey
by1
. If the count ofkey
is0
after the decrement, remove it from the data structure. It is guaranteed thatkey
exists in the data structure before the decrement.getMaxKey()
Returns one of the keys with the maximal count. If no element exists, return an empty string""
.getMinKey()
Returns one of the keys with the minimum count. If no element exists, return an empty string""
.Note that each function must run in
O(1)
average time complexity.Example 1:
Constraints:
1 <= key.length <= 10
key
consists of lowercase English letters.dec
,key
is existing in the data structure.5 * 104
calls will be made toinc
,dec
,getMaxKey
, andgetMinKey
.