我编写了以下方法来确定一个长单词是否包含一个较短的单词,并且我传递字母的顺序似乎会影响结果。
我注意到,如果我喂它absconds
并且bassy
它正确报告NO
,但如果我按字母顺序排列字母并给出它abcdnoss
,abssy
它给出YES
。我不太确定这是为什么 - 任何人都可以发现这个问题吗?
- (BOOL) does: (NSString* ) longWord contain: (NSString *) shortWord {
while([longWord length] > 0 && [shortWord length] > 0) {
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString: [shortWord substringToIndex: 1]];
if ([longWord rangeOfCharacterFromSet: set].location == NSNotFound) {
return NO;
}
longWord = [longWord substringFromIndex: [longWord rangeOfCharacterFromSet: set].location+1];
shortWord = [shortWord substringFromIndex: 1];
}
return YES;
}