直接上代码
//调用返回结果
NSArray *arr = [self findSequentialStrings:@"dcbabcdcba12356543" subStrMinLen:4];
NSLog(@"%@",arr);
//MARK: - 传入一组字符串originalStr,和子字符串长度subStrMinLen,返回查找的子字符串数组
- (NSArray *)findSequentialStrings:(NSString *)originalStr subStrMinLen:(NSInteger)subStrMinLen
{
if (originalStr.length < subStrMinLen) return nil;
NSInteger i = 0;
NSMutableString *appendStr = nil;
BOOL isNeedRefresh = NO;
NSMutableArray *subStrArr = @[].mutableCopy;
for (; i < originalStr.length;) {
if (!isNeedRefresh && [self findSingleSequentialSubStrWithStartIndex:i originalStr:originalStr subStrMinLen:subStrMinLen]) {
if (!appendStr) {
appendStr = [NSMutableString string];
}
[appendStr appendString:[originalStr substringWithRange:NSMakeRange(i, subStrMinLen)]];
i += subStrMinLen;
isNeedRefresh = YES;
if (i == originalStr.length) {
[subStrArr addObject:appendStr.mutableCopy];
}
}else{
if (appendStr.length == 0) {
isNeedRefresh = NO;
++ i;
}else{
char c1 = [originalStr characterAtIndex:i-1];
NSNumber *cNumber1 = [NSNumber numberWithChar:c1];
char c2 = [originalStr characterAtIndex:i];
NSNumber *cNumber2 = [NSNumber numberWithChar:c2];
char startChar = [appendStr characterAtIndex:0];
char oChar = [appendStr characterAtIndex:1];
NSInteger diffNum = [[NSNumber numberWithChar:oChar] integerValue] - [[NSNumber numberWithChar:startChar] integerValue];
if (diffNum > 0) {
if ([cNumber2 integerValue] - [cNumber1 integerValue] == 1) {
[appendStr appendString:[originalStr substringWithRange:NSMakeRange(i, 1)]];
isNeedRefresh = !(i == originalStr.length - 1);
++ i;
}else{
isNeedRefresh = NO;
}
}else if (diffNum < 0) {
if ([cNumber2 integerValue] - [cNumber1 integerValue] == -1) {
[appendStr appendString:[originalStr substringWithRange:NSMakeRange(i, 1)]];
isNeedRefresh = !(i == originalStr.length - 1);
++ i;
}else{
isNeedRefresh = NO;
}
}
if (appendStr.length > 0 && isNeedRefresh == NO) {
[subStrArr addObject:appendStr];
appendStr = nil;
}
}
}
}
if (subStrArr.count == 0) {
return nil;
}
return subStrArr.copy;
}
#pragma mark - private method
//MARK: - 根据起始位置startIndex和要求的子字符串长度subStrMinLen,判断是否满足要求
- (BOOL)findSingleSequentialSubStrWithStartIndex:(NSInteger)startIndex originalStr:(NSString *)originalStr subStrMinLen:(NSInteger)subStrMinLen
{
if (startIndex + subStrMinLen > originalStr.length) {
return NO;
}
//起始位置的字符
char startChar = [originalStr characterAtIndex:startIndex];
//
char minLenChar = [originalStr characterAtIndex:startIndex+subStrMinLen-1];
//判断下标startIndex+subStrLen对应的字符的ASCII值与下标startIndex对应的ASCII值相差是否为subStrLen,相等再继续比较中间子字符串是否连续
NSInteger minLenCharValue = [[NSNumber numberWithChar:minLenChar] integerValue];
NSInteger startCharValue = [[NSNumber numberWithChar:startChar] integerValue];
NSInteger diffNum = minLenCharValue - startCharValue;
if (diffNum == subStrMinLen-1 || diffNum == -(subStrMinLen-1)) {
if (abs((int)diffNum) == 1) {
return YES;
}
if ([self isSubSectionSequential:[originalStr substringWithRange:NSMakeRange(startIndex, subStrMinLen-2)] diffNum:diffNum]) {
//连续的递增或递减
return YES;
}
}
return NO;
}
//MARK: - 判断区间[startIndex, startIndex+subStrMinLen-2]是否是连续的
- (BOOL)isSubSectionSequential:(NSString *)subStr diffNum:(NSInteger)diffNum
{
BOOL isSequential = YES;
for (NSInteger i = 0; i < subStr.length - 1; i++) {
char c1 = [subStr characterAtIndex:i];
NSNumber *cNumber1 = [NSNumber numberWithChar:c1];
char c2 = [subStr characterAtIndex:i+1];
NSNumber *cNumber2 = [NSNumber numberWithChar:c2];
if (diffNum > 0) {
if ([cNumber2 integerValue] - [cNumber1 integerValue] != 1) {
isSequential = NO;
}
}else{
if ([cNumber2 integerValue] - [cNumber1 integerValue] != -1) {
return NO;
}
}
}
return isSequential;
}
打印结果:
(
dcba,
dcba,
6543
)