美文网首页
iOS 获取最长公共字串

iOS 获取最长公共字串

作者: warm_iOS | 来源:发表于2021-06-07 16:11 被阅读0次
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *oneString = @"dfjkbnlskbanvjkfnb";
        NSString *twoString = @"vkjbnkdfdsfknbg ";
        NSLog(@"oneString====> %@ \n twoString====> %@ ",oneString,twoString);
        NSString *maxString = oneString;
        NSString *smallString = twoString;
        if (oneString.length < twoString.length) {
            maxString = twoString;
            smallString = oneString;
        }
        [self findMaxSubString:maxString smallString:smallString];
    }
    - (void)findMaxSubString:(NSString *)maxString smallString:(NSString *)smallString {
        NSString *printSubString = @"";
        for (int i = 0; i <smallString.length; i++) {
            NSString *str = [smallString substringWithRange:NSMakeRange(i, 1)];
            if ([maxString containsString:str] && ![printSubString containsString:str]) {
                printSubString = [printSubString stringByAppendingString:str];
            }
        }
        NSLog(@"printSubString====> %@",printSubString);
    }
    

    相关文章

      网友评论

          本文标题:iOS 获取最长公共字串

          本文链接:https://www.haomeiwen.com/subject/zpbleltx.html