查找字符串中重復(fù)字符,下標(biāo),重復(fù)個數(shù)..?
思路:一遍循環(huán)將字符串轉(zhuǎn)換為字典,字典中保存想要的結(jié)果集
1,最外層是數(shù)組,保證輸入順序與字符串中出現(xiàn)順序一致.
2,獲取數(shù)組lastObject
3,比較lastObject的key,與當(dāng)前字符是否相同, 相同:數(shù)據(jù)加
4,key不同,判斷出現(xiàn)次數(shù),lenth==1,替換, >1新增.
//1.聲明一個共用數(shù)組,self.arrayM: NSMutableArray
//2.code
//3.kvc獲取結(jié)果集
- (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) {//說明上一個元素與當(dāng)前元素重復(fù)
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);
}