两个字符串中搜索最大长度的相同部分

2019-03-11  本文已影响0人  简单易写
//搜索最长相同字符串
- (void)searchTheEqualString{
    NSString *str1 = @"wefzcbrtjsefrasrqehjryjrdfgsd";
    NSString *str2 = @"krdfbdmrrgsefrwhrndxcvsdbxdfq";
    
    NSString *needStr;
    NSInteger max = str1.length;
    for (NSInteger i = 1; i <= max; i ++) {
        for (NSInteger j = 0; j < max-i; j ++) {
            NSRange range = NSMakeRange(j, i);
            NSString *sub = [str1 substringWithRange:range];
            if ([str2 containsString:sub]) {
                needStr = sub;
                break;
            }
        }
    }
    NSLog(@"needStr = %@",needStr);
}
上一篇下一篇

猜你喜欢

热点阅读