Length of Last Word

![](media/15162389890253/15162389958774.jpg)
class Solution {
public:
    int lengthOfLastWord(string s) {
        int len = s.size();
        int cout = 0;
        for (int i=len-1; i>=0; i--) {
            if(cout>0&&s[i]==' ') break;

            if (s[i]==' ') {
                continue;
            }else{
                cout++;
            }

        }
        return cout;
    }
};
2018/1/18 posted in  leetcode

Find matching parenthesis


#import <Foundation/Foundation.h>

int findParenMatch(NSString *s, int n) {
    int res = -1;
    NSString *tmp = [s substringWithRange:NSMakeRange(n, 1)];
    if ([tmp isEqualToString:@"("]) {
        int logF = 0;
        for (int i = n+1; i<s.length; i++) {
            NSString *tmpStrF = [s substringWithRange:NSMakeRange(i, 1)];
            if ([tmpStrF isEqualToString:@"("]) logF++;
            if ([tmpStrF isEqualToString:@")"]) {
                logF--;
                if (logF<0) {
                    res = i;
                    break;
                }
            }
        }
    }else if ([tmp isEqualToString:@")"]){
        int logB = 0;
        for (int i = n-1; i>=0; i--) {
            NSString *tmpStrB = [s substringWithRange:NSMakeRange(i, 1)];
            if ([tmpStrB isEqualToString:@")"]) logB++;
            if ([tmpStrB isEqualToString:@"("]) {
                logB--;
                if (logB<0) {
                    res = i;
                    break;
                }
            }
        }
    }
    return res;
}
2018/1/10 posted in  codeWars

Where my anagrams at?

#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;
}
2018/1/6 posted in  codeWars

Perimeter of squares in a rectangle

#import <Foundation/Foundation.h>

unsigned long long perimeter(int n) {
    NSMutableArray *numQu = @[@0,@1].mutableCopy;
    unsigned long long n0 = 0,n1=1;
    for (int i = 2; i<=n+1; i++) {
        unsigned long long sum = n0+n1;
        n0 = n1;
        n1 = sum;
        [numQu addObject:@(sum)];
    }
    unsigned long long tol = 0;
    for (NSNumber* ele in numQu) {
        tol+=[ele unsignedLongLongValue];
    }
    return tol*4;
}
2018/1/6 posted in  codeWars

1's, 0's and wildcards

#import <Foundation/Foundation.h>

NSArray *changeTo(NSMutableArray*log, NSMutableArray*res){
    if (log.count>0) {
        NSRange loc = {[[log lastObject] integerValue],1};
        NSMutableArray *cop = res.mutableCopy;
        for (NSString *tmp in res) {
            [cop addObject:[tmp stringByReplacingCharactersInRange:loc withString:@"0"]];
            [cop addObject:[tmp stringByReplacingCharactersInRange:loc withString:@"1"]];
            [cop removeObject:tmp];
        }
        [log removeLastObject];
        return changeTo(log, cop);
    }else{
        return res;
    }
}

NSArray *possibilities(NSString *s) {
    NSMutableArray *log = @[].mutableCopy;
    NSMutableArray *res = @[].mutableCopy;
    for (int i = 0; i<s.length; i++) {
        if ([s characterAtIndex:i]=='?') {
            [log addObject:@(i)];
        }
    }
    [res addObject:s];

    NSArray *tol = changeTo(log , res);
    
    return tol;
}

简化版:

#import <Foundation/Foundation.h>

NSArray *possibilities(NSString *s) {
  // NSLog(@"User `possibilities` accidentally invoked!\n");
  if ([s rangeOfString: @"?"].length == 0) return @[s];
  NSString *r = [s stringByReplacingOccurrencesOfString: @"?" withString: @"0" options: 0 range: [s rangeOfString: @"?"]], *t = [s stringByReplacingOccurrencesOfString: @"?" withString: @"1" options: 0 range: [s rangeOfString: @"?"]];
  return [possibilities(r) arrayByAddingObjectsFromArray: possibilities(t)];
}
2017/12/29 posted in  codeWars

Simple Pig Latin

一开始的复杂版:

#import <Foundation/Foundation.h>

NSString *pigIt(NSString *s) {
    NSMutableArray *str = [s componentsSeparatedByString:@" "].mutableCopy;
    NSMutableString *res = @"".mutableCopy;
    for (NSString *tmp in str) {
        unichar c = [tmp characterAtIndex:0];
        if ((c>='a'&&c<='z')||(c>='A'&&c<='Z')) {
            NSMutableString *change = tmp.mutableCopy;
            [change appendFormat:@"%c",c];
            if ([tmp isEqualToString:[str lastObject]]) {
                [change appendString:@"ay"];
            }else{
                [change appendString:@"ay "];
            }
            [res appendString:[change substringFromIndex:1]];
        }else{
            [res appendFormat:@"%c",c];
        }
    }
    return res;
}

简化版

#import <Foundation/Foundation.h>

NSString *pigIt(NSString *s) {
  NSMutableArray *result = [s componentsSeparatedByString: @" "];
  for (size_t i = 0; i < [result count]; i++) result[i] = [[[result[i] substringFromIndex: 1] stringByAppendingString: [result[i] substringToIndex: 1]] stringByAppendingString: @"ay"];
  return [result componentsJoinedByString: @" "];
}
2017/12/27 posted in  codeWars