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.119 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.118 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.117 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.116 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.115 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.114 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.113 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.111 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.110 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.109 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.108 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.107 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.106 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.105 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.104 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.103 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.102 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.101 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.100 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.99 tracks/objective-c/exercises/bracket-push/BracketPushExample.m