Sha256: 23615433547c9230f7cfbc0f5b122918b0e333d2769e484892ff7ed076dbf592

Contents?: true

Size: 1.54 KB

Versions: 262

Compression:

Stored size: 1.54 KB

Contents

#import "BracketPushExample.h"

@implementation BracketPushExample

+ (BOOL)validateBracketPairingAndNestingInString:(NSString *)string {
    NSDictionary *brackets = @{@")" : @"(",
                               @"]" : @"[",
                               @"}" : @"{" };
    
    NSMutableArray *stack = [[NSMutableArray alloc] init];
    NSRange textRange = NSMakeRange(0, string.length);
    __block BOOL mismatch = NO;
    
    [string enumerateSubstringsInRange:textRange
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString * _Nullable
                                         substring,
                                         NSRange substringRange,
                                         NSRange enclosingRange,
                                         BOOL * _Nonnull stop)
     {
         if ([[brackets allValues] containsObject:substring]) {
             [stack addObject:substring];
         } else if ([[brackets allKeys] containsObject:substring]) {
             NSString *last = [stack lastObject];
             
             if (last) {
                 [stack removeLastObject];
                 
                 if (![last isEqualToString:brackets[substring]]) {
                     mismatch = YES;
                     *stop = YES;
                 }
             } else {
                 mismatch = YES;
                 *stop = YES;
             }
         }
     }];
    
    if (mismatch) {
        return NO;
    }
    
    return [stack count] == 0;
}

@end

Version data entries

262 entries across 262 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.179 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.178 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.177 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.176 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.175 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.174 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.173 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.172 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.171 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.170 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.169 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.167 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.166 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.165 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.164 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.163 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.162 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.161 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.160 tracks/objective-c/exercises/bracket-push/BracketPushExample.m