假设有一个字符串aabcad,请写一段程序,去掉字符串中不相邻的重复字符串,即上述字符串处理之后的输出结果为:aabcd

这是一道关于字符串处理的问题,要求移除不相邻的重复字符,例如字符串'aabcad'处理后应为'aabcd'。文章指出网上常见解答错误,并提供了作者自己的代码实现。

朋友给我发了这个题目, 一看这种问题肯定属于网红题, 肯定网上有很多解答, 有了思路后就想网搜一下看看大家有没有其它想法, 结果翻了一圈下来, 看到的解答都是不正确的, 就自己写了些代码放上来, 仅供参考:

注: 不仅仅是要aabcad->aabcd这种结果, 所求是移除所有有重复但不相邻的, 如无相邻的则不管, 如abab->abab

/*
 aabcad -> aabcd, abbdcaabd -> bbdcaad ...
 思路:
 1. 获取每个字符出现的次数;
 2. 定位个数为2以上的每个字母的位置
 3. 记录要删除字符的位置
 4. 对应删除
 */
- (NSString *)removeTheEqualButNotNearbyCharForStr:(NSString *)fromStr {
    if (fromStr.length <= 3) return fromStr;
    
    // 统计每个字符出现的次数
    NSMutableDictionary *chatCountDict = [[NSMutableDictionary alloc] init];
    int count = 0; BOOL equal = NO;
    for (int i = 0; i < fromStr.length; i++) {
        count = 0; equal = NO;
        for (int j = 0; j < i; j++) {
            if ([fromStr characterAtIndex:i] == [fromStr characterAtIndex:j]) {
                equal = YES;
                break;
            }
        }
        if (equal) continue;
        for (int j = i; j < fromStr.length; j++) {
            if ([fromStr characterAtIndex:i] == [fromStr characterAtIndex:j]) count++;
        }
        [chatCountDict setValue:@(count) forKey:[fromStr substringWithRange:NSMakeRange(i, 1)]];
    }
    
    // 保留2个以上的字符记录
    [chatCountDict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([obj intValue] <= 2) [chatCountDict removeObjectForKey:key];
    }];
    
    // 获取字符对应位置
    NSMutableDictionary *chatLocationDict = [[NSMutableDictionary alloc] init];
    [chatCountDict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL * _Nonnull stop) {
        NSMutableArray *locationArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < fromStr.length; i++) {
            if ([[NSString stringWithFormat:@"%c",[fromStr characterAtIndex:i]] isEqualToString:key]) [locationArr addObject:@(i)];
        }
        [chatLocationDict setValue:locationArr forKey:key];
    }];
    
    // 记录要删除字符的位置
    NSMutableArray *indexArrM = [[NSMutableArray alloc] init];
    for (int j = 0; j < chatLocationDict.allKeys.count; j++) {
        NSArray *locaArr = chatLocationDict[chatLocationDict.allKeys[j]];
        BOOL hasEqualNearBy = NO;
        for (int i = 0; i < locaArr.count - 1; i++) {
            if ([locaArr[i] intValue] + 1 == [locaArr[i + 1] intValue]) hasEqualNearBy = YES;
        }
        if (!hasEqualNearBy) continue;
        for (int i = 0; i < locaArr.count; i++) {
            if (
                (i == 0 && ([locaArr[i] intValue] + 1 != [locaArr[i + 1] intValue])) ||
                (i == locaArr.count -1 && ([locaArr[i - 1] intValue] + 1 != [locaArr[i] intValue])) ||
                (i != 0 && i != locaArr.count - 1 && (([locaArr[i] intValue] + 1 != [locaArr[i + 1] intValue]) && ([locaArr[i - 1] intValue] + 1 != [locaArr[i] intValue])))
                ) {
                [indexArrM addObject:locaArr[i]];
            }
        }
    }
    
    // 移除
    NSMutableArray *arrM = [[NSMutableArray alloc] initWithCapacity:fromStr.length];
    for (int i = 0; i < fromStr.length; i++) [arrM addObject:[NSString stringWithFormat:@"%c",[fromStr characterAtIndex:i]]];
    NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
    for (NSNumber *num in indexArrM) [indexSet addIndex:[num integerValue]];
    [arrM removeObjectsAtIndexes:indexSet];
    
    NSMutableString *strM = [[NSMutableString alloc] init];
    for (NSString *str in arrM) [strM appendString:str];
    return strM.copy;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值