Is an array preferable to a set or map?

I recently spoke with a company in the Bay Area (California, USA). One of the questions was simply to find if the string contains duplicate characters (I simplified the lengthy question).

eg:
input : "qwerrty"
output : True

I used python to encode this.

I gave a solution that uses a kit to track items that occur during iteration.

However, the interviewer wanted me to use an array [255], which tracks the characters encountered.

Although it was very convenient for me to use any of them, my opinion was to use the set simply because we lose 255 characters when we use the array. This is because (as we all know) initially we create arr [255] = 0, all elements are equal to zero, and then we increase the value of the equivalent ASCII index by 1.

On the other hand, a set will spend memory only on visited items.

Since he (sort of) claimed to be using an array over a set, I am curious to find out if it was technically correct. Is the array preferable over the set / map in this case? If so, why?

+4
source share
2 answers

, C , , C + 1 , , , , ( ). .

, . , , (0) (1), . , . W, , C/W .

, C = 256 (, , ) 32- (W = 32). , , 0. . -, - . -, -. - -, . - , , , - , . - char , (20 ) - , , , , , 10 . , , -, , , . , , .

, , Unicode . , C = 1,114,112 (, Wikipedia), 64- 17 408 . , . , , , "", , , ( , & radic; (2C), ), -, , . , , -, - .., , , , , .

:

  • (, ASCII), , , .

  • ( Unicode), , , , , -.

, , , , " ", . " , , , ".

+3

, - , . , - O (N). . O (1), 1 - O (1). , , , .

+2

All Articles