determine if a string has all unique characters?

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

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

16条回答
  •  旧时难觅i
    2020-12-28 09:15

    If you are talking about an ASCII string:

    1. Create an int array [0-255], one for each character index, initialised to zero.

    2. Loop through each character in the string and increment the respective array position for that character

    3. If the array position already contains a 1, then that character has already been encountered. Result => Not unique.

    4. If you reach the end of the string with no occurrence of (3), Result => the string is unique.

提交回复
热议问题