NSString* str =@"aabcad";
NSMutableArray* cArr =@[].mutableCopy;
for(int i =0; i < str.length; i++) {
[cArr addObject:[str substringWithRange:NSMakeRange(i,1)]];}
NSMutableArray* deArr =@[].mutableCopy;
for(int i =0; i < cArr.count; i++) {
for(int j =0; j < cArr.count; j++) {
if([cArr[i] isEqualToString:cArr[j]] && i!=j) {
if(i ==0) {
if(![cArr[i] isEqualToString:cArr[i+1]]) {
[deArr addObject:@(i)];
break;}}
if(i >0&& i < cArr.count-1){
if(![cArr[i] isEqualToString:cArr[i+1]] && ![cArr[i] isEqualToString:cArr[i-1]]) {
[deArr addObject:@(i)];
break;}}
if(i == cArr.count-1) {
if(![cArr[i] isEqualToString:cArr[i-1]]) {
[deArr addObject:@(i)];
break;}}
}
}
}
for(int i = (int)deArr.count-1; i >=0; i--) {
[cArr removeObjectAtIndex:[deArr[i] integerValue]];
}
NSString* newStr = [cArr componentsJoinedByString:@""];
NSLog(@"%@",str);
NSLog(@"%@",newStr);