read010931
BAN USER//
// main.m
// CommondLineProject
//
// Created by Dun Liu on 6/23/14.
// Copyright (c) 2014 Dun Liu. All rights reserved.
//
#import <Foundation/Foundation.h>
/*
given 2 arrays wrds[] , chars[] as an input to a function such that
wrds[] = [ "abc" , "baa" , "caan" , "an" , "banc" ]
chars[] = [ "a" , "a" , "n" , "c" , "b"]
Function should return the longest word from words[] which can be constructed from the chars in chars[] array.
for above example - "caan" , "banc" should be returned
Note: Once a character in chars[] array is used, it cant be used again.
eg: words[] = [ "aat" ]
characters[] = [ "a" , "t" ]
then word "aat" can't be constructed, since we've only 1 "a" in chars[].
*/
@interface Handler : NSObject
+ (NSArray *)longestWords:(NSArray *)words useChars:(NSArray *)chars;
@end
@implementation Handler
+ (NSArray *)longestWords:(NSArray *)words useChars:(NSArray *)chars {
NSMutableArray *result = [NSMutableArray new];
NSUInteger longest = 0;
NSArray *sortedChars= [ chars sortedArrayUsingSelector:@selector(compare:)];
for (NSString *word in words) {
NSMutableArray *wordChars = [NSMutableArray new];
for (int i=0; i<word.length; i++) {
[wordChars addObject:[word substringWithRange:NSMakeRange(i,1)]];
}
[wordChars sortUsingSelector:@selector(compare:)];
if ([self array:wordChars isSubArrayOf:sortedChars]) {
if (wordChars.count > longest ) {
[result removeAllObjects];
longest = wordChars.count;
}
if (wordChars.count == longest ) {
[result addObject:word];
}
}
}
return result;
}
+ (BOOL)array:(NSArray *)a1 isSubArrayOf:(NSArray *)a2 {
NSUInteger i1=0, i2=0;
while(i1<a1.count && i2<a2.count) {
if ([a1[i1] isEqualToString:a2[i2]]) {
i1++;
i2++;
} else {
i2++;
}
}
return i1==a1.count;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *words = @[ @"abc" , @"baa" , @"caan" , @"an" , @"banc" ];
NSArray *chars = @[ @"a" , @"a" , @"n" , @"c" , @"b"];
NSLog(@"%@", [Handler longestWords:words useChars:chars]);
}
return 0;
}
problem of the question: if even and equal size, there will be definitely no middle number.
- read010931 May 06, 2014NSString *input = @"johndoe_sdfjkldfj_ldfjlkfdsjfds_dfdjklfdsjs.txt";
input = [input substringToIndex:input.length-4];
NSArray *components = [input componentsSeparatedByString:@"_"];
NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithCapacity:components.count];
if (components.count>0) {
NSString *name = components[0];
result[@"NAME"]=name;
for (int i = 1; i<components.count; i++) {
NSString *component = components[i];
NSString *key = [component substringToIndex:1];
NSString *value = [component substringFromIndex:1];
result[key]=value;
}
}
NSLog(@"%@", result);
If duplicate token, I will not use the token as the key, I will use index as the key. Then make another mapping from index to token.
- read010931 May 06, 2014Yes. There's no technique to cancel blocks which were defined within a dispatch_after call.
But you can use a boolean that wrap the block to determine whether execute in the block or not.
vmagaziy 's solution is impressive!
I have a solution is more algorithm involved.
- (NSArray *)mergeArray:(NSArray *)arr {
NSMutableArray *mArr = [arr mutableCopy];
while (mArr.count >1) {
NSMutableArray *result = [NSMutableArray array];
for (int i =0; i<mArr.count/2; i++) {
NSMutableArray *arr0 = mArr[2*i];
NSMutableArray *arr1 = mArr[2*i+1];
NSMutableArray *merge = [[NSMutableArray alloc] initWithCapacity:arr0.count+arr1.count];
int i0 =0;
int i1 =0;
while (i0 < arr0.count || i1 < arr1.count) {
if (i0==arr0.count || arr1[i1]<arr0[i0]) {
[merge addObject:arr1[i1++]];
} else {
[merge addObject:arr0[i0++]];
}
}
[result addObject:merge];
}
mArr = result;
}
return mArr[0];
}
So smart! Just wrote it in another way:
- read010931 July 15, 2014