函數(shù)功能如下:
對于輸入的字符串s列赎,忽略大小寫表鳍,返回按照英文字母出現(xiàn)的頻率從高到低排序的小寫字符串团甲,對于出現(xiàn)次數(shù)相同的字母逾冬,按照字典序排序。如果輸入的字符串不包含英文字母躺苦,返回空字符串身腻。注意數(shù)字,標點匹厘,空格都不是英文字母嘀趟。
例如:輸入字符:We Attack at Dawn ,輸出:atwcdekn
解釋:在這個例子中不區(qū)分大小寫愈诚,字母 a 出現(xiàn)了3次去件,字母 t 出現(xiàn)了3次,字母 w 出現(xiàn)2次扰路,字母 c,d,e,k,n 均出現(xiàn)一次尤溜,對于出現(xiàn)次數(shù)相同的字母按照字典書序排序,例如按照字典序字母 a 排在字母 t 之前汗唱,所以輸出結果為:atwcdekn
代碼實現(xiàn)
void PrintOut(NSString *text){
NSString *result = @"";
NSMutableArray *array1 = [NSMutableArray array];
//取出小寫字母放入數(shù)組中
text = text.lowercaseString;
for(int i =0; i < [text length]; i++){
char character = [text characterAtIndex:i];
if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:character]) {
[array1 addObject:[NSString stringWithFormat:@"%c",character]];
}
}
// 相同的字符放入同一個數(shù)組中變?yōu)槎S數(shù)組
NSMutableArray *dateMutablearray = [@[] mutableCopy];
NSMutableArray *array = [NSMutableArray arrayWithArray:array1];
for (int i = 0; i < array.count; i ++) {
NSString *string = array[i];
NSMutableArray *tempArray = [@[] mutableCopy];
[tempArray addObject:string];
for (int j = i+1; j < array.count; j ++) {
NSString *jstring = array[j];
if([string isEqualToString:jstring]){
[tempArray addObject:jstring];
[array removeObjectAtIndex:j];
}
}
[dateMutablearray addObject:tempArray];
}
// NSLog(@"dateMutable:%@",dateMutablearray);
// 按照二維數(shù)組元素個數(shù)排序
for (int i = 0; i < dateMutablearray.count; i++) {
for (int j = i+1; j < dateMutablearray.count; j++) {
NSInteger inum = ((NSArray*)dateMutablearray[i]).count;
NSInteger jnum = ((NSArray*)dateMutablearray[j]).count;
if (inum < jnum) {
[dateMutablearray exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
NSLog(@"dateMutable:%@",dateMutablearray);
BOOL continueNext = YES;
//把二維數(shù)組中數(shù)組個數(shù)相同的放入一個數(shù)組中
NSMutableArray *numberarray = [@[] mutableCopy];
for (int i = 0; i < dateMutablearray.count; i ++) {
NSArray *arr = dateMutablearray[i];
NSInteger num = arr.count;
NSMutableArray *tempArray = [@[] mutableCopy];
NSString *istr = arr[0];
[tempArray addObject:istr];
if (num == 1) {
if (continueNext) {
for (int j = i+1; j < dateMutablearray.count; j ++) {
NSArray *jarr = dateMutablearray[j];
// NSInteger jnum = jarr.count;
NSString *jstr = jarr[0];
[tempArray addObject:jstr];
}
[numberarray addObject:tempArray];
}
continueNext = NO;
}else{
for (int j = i+1; j < dateMutablearray.count; j ++) {
NSArray *jarr = dateMutablearray[j];
NSInteger jnum = jarr.count;
NSString *jstr = jarr[0];
if(num == jnum){
[tempArray addObject:jstr];
[dateMutablearray removeObjectAtIndex:j];
}
}
[numberarray addObject:tempArray];
}
}
NSLog(@"numberarray:%@",numberarray);
// 二維數(shù)組中每個數(shù)組的元素排序
NSMutableArray *sortarray = [@[] mutableCopy];
for (int i = 0; i < numberarray.count; i++) {
NSArray *arr = numberarray[i];
NSArray *newArray = [arr sortedArrayUsingSelector:@selector(compare:)];
[sortarray addObject:newArray];
}
NSLog(@"sortarray:%@",sortarray);
// 拼接所有數(shù)組所有元素
for (int i = 0; i < sortarray.count; i++) {
NSArray *arr = sortarray[i];
for (int j = 0; j < arr.count; j++) {
result = [result stringByAppendingString:arr[j]];
}
}
NSLog(@"result:%@",result);
}
以上代碼實現(xiàn)可能思路比較按部就班宫莱,歡迎大家提供更好的方法交流