Where my anagrams at?

2018/1/6 posted in  codeWars

#import <Foundation/Foundation.h>

NSArray *anagrams(NSString *s, NSArray *a) {
    NSMapTable *map = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableStrongMemory];
    for (int i = 0; i<[s length]; i++) {
        NSString *str = [s substringWithRange:NSMakeRange(i, 1)];
        if ([map objectForKey:str]) {
            [map setObject:@([[map objectForKey:str] intValue]+1) forKey:str];
        }else{
            [map setObject:@(1) forKey:str];
        }
    }
    NSMutableArray *res = a.mutableCopy;
    for (NSString *ele in a) {
        if(ele.length!=s.length){
            [res removeObject:ele];
            continue;
        }
        NSMapTable *tmp = [map copy];
        for (int j=0; j<[ele length]; j++) {
            NSString *tmpStr = [ele substringWithRange:NSMakeRange(j, 1)];
            if ([tmp objectForKey:tmpStr]) {
                int log =[[tmp objectForKey:tmpStr] intValue]-1;
                if (log<0){
                    [res removeObject:ele];
                    break;
                }
                [tmp setObject:@(log) forKey:tmpStr];
            }else{
                [res removeObject:ele];
                break;
            }
        }
    }
    return res;
}