查找字符串中重复字符,下标,重复个数..?
思路:一遍循环将字符串转换为字典,字典中保存想要的结果集
1,最外层是数组,保证输入顺序与字符串中出现顺序一致.
2,获取数组lastObject
3,比较lastObject的key,与当前字符是否相同, 相同:数据加
4,key不同,判断出现次数,lenth==1,替换, >1新增.
//1.声明一个共用数组,self.arrayM: NSMutableArray
//2.code
//3.kvc获取结果集
- (void)repeatInfoIn:(NSString *)string
{
NSInteger count = string.length;
if (count <= 1) return;
for (NSInteger i = 0; i < string.length; i++) {
NSString *ichar = [string substringWithRange:NSMakeRange(i, 1)];
if (i == 0) {
NSDictionary *valueDict = @{@"key": ichar, @"index": @(i), @"count": @(1), @"subStr": ichar};
[self.arrayM addObject:valueDict];
continue;
}
NSMutableDictionary *previousDict = [[self.arrayM lastObject] mutableCopy];
NSString *previousKey = previousDict[@"key"];
NSInteger previousCount = [previousDict[@"count"] integerValue];
if ([previousKey isEqualToString:ichar]) {
previousCount += 1;
NSString *subStr = [NSString stringWithFormat:@"%@%@", previousDict[@"subStr"], ichar];
previousDict[@"count"] = @(previousCount);
previousDict[@"subStr"] = subStr;
[self.arrayM replaceObjectAtIndex:(self.arrayM.count-1) withObject:previousDict];
}else {
if (previousCount == 1) {
NSDictionary *valueDict = @{@"key": ichar, @"index": @(i), @"count": @(1), @"subStr": ichar};
[self.arrayM replaceObjectAtIndex:(self.arrayM.count-1) withObject:valueDict];
}else {
NSDictionary *valueDict = @{@"key": ichar, @"index": @(i), @"count": @(1), @"subStr": ichar};
[self.arrayM addObject:valueDict];
}
}
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSString *string = @"asdqwezccdddd";
[self repeatInfoIn:string];
NSLog(@"%@", self.arrayM);
NSArray *subStrArray = [self.arrayM valueForKeyPath:@"subStr"];
NSLog(@"%@", subStrArray);
}
/* java中可能有更简便的方法,我这里只是一种思路推演. */
static void linkedHashMapOperate() {
String str = "aaasdqwezccdddd";
ArrayList<HashMap<Character,HashMap<String, Object>>> list = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
char key = str.charAt(i);
int lastIndex = Math.max(0, list.size()-1);
if (i == 0) {
HashMap<Character, HashMap<String, Object>> hashMap = new HashMap<>();
var value = new HashMap<String, Object>();
value.put("index", i);
value.put("lenth", 1);
value.put("key", key);
hashMap.put(key, value);
list.add(hashMap);
continue;
}
HashMap<Character, HashMap<String, Object>> hashMap = list.get(lastIndex);
Set<Map.Entry<Character, HashMap<String, Object>>> entries = hashMap.entrySet();
char lastKey = key;
var value = new HashMap<String, Object>();
for (Map.Entry<Character, HashMap<String, Object>> entry: entries) {
lastKey = (char) entry.getKey();
value = entry.getValue();
}
if (lastKey == key) {//说明上一个元素与当前元素重复
int index = (int) value.get("index");
int lenth = (int) value.get("lenth");
index += 1;
lenth += 1;
value.put("index", index);
value.put("lenth", lenth);
}else {
//判断上一个元素lenth==1
int lenth = (int) value.get("lenth");
if (lenth==1) {//替换--->key
value.put("key",key);
hashMap.remove(lastKey);
hashMap.put(key,value);
list.set(lastIndex, hashMap);
}else {//新增
HashMap<Character, HashMap<String,Object>> nHashMap = new HashMap();
var nValue = new HashMap<String, Object>();
nValue.put("index", i);
nValue.put("lenth", 1);
nValue.put("key", key);
nHashMap.put(key, nValue);
list.add(nHashMap);
}
}
}
System.out.println(list);
}