I was reading thea solution here (https://leetcode.com/problems/longest-substring-without-repeating-characters/solution/).They say that in normal cases when we havethere is no idea about key's (character's) range we use hash maphashmap. And
And there is another solution which says that when we are particular about the character set we can use array instead.
The previous implements all have no assumption on the charset of the string s. If we know that the charset is rather small, we can replace the Map with an integer array as direct access table. Commonly used tables are: int[26] for Letters 'a' - 'z' or 'A' - 'Z' int[128] for ASCII int[256] for Extended ASCII The previous implements all have no assumption on the charset of the string s.
If we know that the charset is rather small, we can replace the Map with an integer array as direct access table.
Commonly used tables are:
int[26] for Letters 'a' - 'z' or 'A' - 'Z' int[128] for ASCII int[256] for Extended ASCII
Not only here, but also in several locations in Cracking the Coding interview book they use an array where the characters are keys by representing a character by the equivalent integer and storing the value at a corresponding location in that array.
Why would we struggle with array's when we have hashmap at our hand ? Is there any reason ?