在 Objective-C 中检查字符串中的相同字符
我有一个字符串数组,我想从中仅提取具有唯一字符集的字符串。 (例如,“asdf”和“fdsa”将被视为多余)。这是我目前正在使用的方法:
NSMutableArray *uniqueCharSets = [[NSMutableArray alloc] init];
NSMutableArray *uniqueStrings = [[NSMutableArray alloc] init];
for (NSString *_string in unique) {
NSCharacterSet *_charSet = [NSCharacterSet characterSetWithCharactersInString:_string];
if (![uniqueCharSets containsObject:_charSet]) {
[uniqueStrings addobject:_string];
[uniqueCharSets addObject:_charSet];
}
}
这似乎可行,但它非常慢并且占用资源。有人能想出更好的方法来做到这一点吗?
I have an array of strings, from which I would like to extract only those with unique character sets. (For example, "asdf" and "fdsa" would be considered redundant). This is the method I am currently using:
NSMutableArray *uniqueCharSets = [[NSMutableArray alloc] init];
NSMutableArray *uniqueStrings = [[NSMutableArray alloc] init];
for (NSString *_string in unique) {
NSCharacterSet *_charSet = [NSCharacterSet characterSetWithCharactersInString:_string];
if (![uniqueCharSets containsObject:_charSet]) {
[uniqueStrings addobject:_string];
[uniqueCharSets addObject:_charSet];
}
}
This seems to work, but it's very slow and resource-intensive. Can anyone think of a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSDictionary
,将每个字符串按字典顺序排序的等效项映射到输入字符串的NSArray
:(例如adfs
=>[afsd , asdf, ...]
)NSDictionary
, map each string's lexicographically-sorted equivalent to anNSArray
of input strings: (e.g.adfs
=>[afsd, asdf, ...]
)我只是整理了一个简单的例子来说明我将如何处理这个问题,但事实证明它比你最初预期的更奇怪。其一,
NSCharacterSet
没有实现相等性来检查内容。它仅使用指针值。基于此,您的示例将无法正常工作。我的方法是使用 NSSet 来为我们处理这些哈希值。
代码非常简单。我们创建一个容器对象来缓存字符集位图表示的结果。我们使用位图表示是因为
NSData
适当地实现了isEqual:
。I just put together a quick example of how I would approach this, but it turns out that it is more, odd, than you first expect. For one,
NSCharacterSet
doesn't implement equality to check contents. It only uses the pointer value. Based on this your example will NOT work properly.My approach is to use an NSSet to deal with the hashing of these for us.
The code is pretty straightforward. We create a container object to cache the results of the character set's bitmap representation. We use the bitmap representation because
NSData
implementsisEqual:
appropriately.我唯一想到的是不要使用
containsObject
:由于NSMutableArray
没有排序(一般来说),我们可以假设containsObject
只是从头开始迭代数组,直到找到对象。这意味着O(n)
(最坏情况下进行n
次比较)。更好的解决方案可能包括保持数组有序并使用二分法的自定义搜索方法。这样你的复杂度就会
O(log n)
。当然,您必须注意保持数组有序(比添加和重新排序更有效),因此您应该使用
insertObject:atIndex:
方法正确插入元素。The only thing that come in my mind is not to use
containsObject
: sinceNSMutableArray
is not ordered (in general), we can assume thatcontainsObject
simply iterates the array starting from the beginning until he finds the object. This meansO(n)
(n
comparisons in the worst case).A better solution may consists in keeping the array ordered and use a custom search method using a dichotomic approach. This way you'll have a
O(log n)
complexity.Of course, you must take care of keeping your array ordered (much more efficient than add and reorder), so you should use
insertObject:atIndex:
method to insert the element properly.