确定字符串是否包含所有唯一字符?
谁能告诉我如何实现一个程序来检查字符串包含所有唯一字符?
Can anybody tell me how to implement a program to check a string contains all unique chars ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果您正在谈论 ASCII 字符串:
创建一个 int 数组 [0-255],其中一个
对于每个字符索引,
初始化为零。
循环遍历
字符串中的每个字符和
增加该字符各自的数组位置
如果数组位置已包含 1,则已遇到该字符。结果=>不唯一。
如果你到达终点
没有出现的字符串
(3)、结果=>该字符串是唯一的。
If you are talking about an ASCII string:
Create an int array [0-255], one
for each character index,
initialised to zero.
Loop through
each character in the string and
increment the respective array position for that character
If the array position already contains a 1, then that character has already been encountered. Result => Not unique.
If you reach the end
of the string with no occurrence of
(3), Result => the string is unique.
使用您选择的算法(例如内置的 qsort 函数)对字符串中的字符进行排序,然后扫描字符串检查连续的重复字母;如果到达末尾没有找到任何字符,则该字符串包含所有唯一字符。
另一种方法可能是使用某种结构,该结构对于字符串可能包含的每个字符都有一个存储桶,全部初始化为零;您扫描字符串,增加与当前字符对应的存储桶的值。如果您要增加一个内部已经有 1 的存储桶,则可以确定您的字符串包含重复项。
这对于
char
和数组(大小为UCHAR_MAX+1
)可以很好地工作,但是当您开始处理宽字符时,它很快就会失控。在这种情况下,您将需要一个哈希表或其他一些“严肃”的容器。最好的算法取决于要检查的字符串的长度、每个字符的大小、排序算法的速度以及分配/使用结构来保存字符频率的成本。
Sort the characters in the string using your algorithm of choice (e.g. the builtin
qsort
function), then scan the string checking for consecutive repeating letters; if you get to the end without finding any, the string contains all unique characters.An alternative may be using some structure that has one bucket for each character the string may contain, all initialized to zero; you scan the string, incrementing the value of the bucket corresponding to the current character. If you get to increment a bucket that already has a 1 inside it you are sure that your string contains duplicates.
This can work fine with
char
s and an array (of sizeUCHAR_MAX+1
), but it quickly gets out of hand when you start to deal with wide characters. In such case you would need a hashtable or some other "serious" container.The best algorithm depends on the length of the strings to examine, the size of each character, the speed of the sorting algorithm and the cost of allocating/using the structure to hold the character frequencies.
制作一组字母,并计算值。
set("adoihgoiaheg")
=set(['a', 'e', 'd', 'g', 'i', 'h', 'o'])< /代码>:
Make a set of the letters, and count the values.
set("adoihgoiaheg")
=set(['a', 'e', 'd', 'g', 'i', 'h', 'o'])
:使用 256 项数组。用0填充。现在遍历字符串,如果为0则将数组中相应的条目设置为1。否则,字符串中有重复的字符。
Use a 256-entry array. Fill it with 0. Now traverse the string setting the corresponding entry in the array to 1 if it's 0. Otherwise, there are repeated chars in the string.
将大小等于字符集的布尔数组设置为 false。 (恒定时间)。扫描字符串;对于每个字符,检查该字符槽处的数组;如果为 true,则字符串有重复字符。如果为 false,则将该插槽设置为 true 并继续。如果到达末尾时没有遇到重复字符,则说明不存在重复字符,并且该字符串仅包含唯一字符。运行时间:O(n),当 n 是字符串的长度时,并且常数非常小。
Set an array of booleans of size equal to the character set to false. (Constant time). Scan the string; for each character, inspect the array at the characater's slot; if true, string has duplicate characters. If false, set that slot to true and continue. If you get to the end without encountering a duplicate, there aren't any and the string only contains unique characters. Running time: O(n) when n is the lenght of the string, with a pretty small constant.
类似地(并且没有数组),使用哈希表!
//伪代码:
运行时间是 O(n) 并且内存空间也更好,因为你不需要 256 (asciis) 的数组
Similarly (and without arrays), use a HASH TABLE!
//psuedo code:
Run time is O(n) and memory space is better too since you don't need an array of 256 (asciis)
使用哈希表,添加每个字符的键以及出现次数作为值。循环遍历 HashTable 键以查看是否遇到 count > > 1. 如果是,则输出 false。
Use a HashTable, add the key for each character along with the count of occurrences as the value. Loop through the HashTable keys to see if you encountered a count > 1. If so, output false.
简单的解决方案是使用 2 个循环。
不需要额外的数据结构来跟踪字符。
Simple solution will be using 2 loops.
No additional data structure is needed to keep a track on characters.
我最初的答案也是采用类似的数组技术并计算字符出现的次数。
但我是用 C 语言做的,我认为使用一些指针操作可以很简单并完全摆脱数组
my original answer was also doing the similar array technique and count the character occurrence.
but i was doing it in C and I think it can be simple using some pointer manipulation and get rid of the array totally
不使用额外内存:
Without using additional memory:
我相信有一个更简单的方法:
I beleive there is a much simpler way:
我希望这可以帮助你
}
I hope this can help you
}
这是该问题的最佳解决方案。
它只需要一个整数变量,并且无论字符串大小如何都可以判断它是否唯一。
this is optimal solution for the problem.
it takes only an integer variable and can tell whether it is unique or not regardless of string size.