读书人

unique character in string,该怎么处

发布时间: 2012-02-06 15:52:45 作者: rapoo

unique character in string
Implement an algorithm to determine if a string has all unique characters.
you can not use additional data structures?
请大侠指点
谢谢

[解决办法]
Use hash table to detect if there is repeated characters,When hash collision occurs,there must be characteres that is not unique.
The time complicity and space complexity are both O(n).
[解决办法]
如果允许破坏原来的数据,那么可以排序,在排序中判断是否有相等的,相等则跳出。

由于排序可以达到O(nlogn),所以这个方法不会低于O(nlogn)。

读书人网 >软件架构设计

热点推荐