determine if a string has all unique characters?

前端 未结 16 1292
长发绾君心
长发绾君心 2020-12-28 08:29

Can anybody tell me how to implement a program to check a string contains all unique chars ?

16条回答
  •  醉梦人生
    2020-12-28 09:11

    Similarly (and without arrays), use a HASH TABLE!

    //psuedo code:

    1. go through each char of the string
    2. hash the char and look it up in the hash table
    3. if the table has the hash, return FALSE // since it's not unique
    4. __else store the hash
    5. return to step #1 until you're done

    Run time is O(n) and memory space is better too since you don't need an array of 256 (asciis)

提交回复
热议问题