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.139 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.138 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.137 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.136 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.135 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.134 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.133 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.132 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.131 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.130 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.129 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.128 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.127 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.126 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.125 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.124 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.123 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.122 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.121 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.120 tracks/objective-c/exercises/bracket-push/BracketPushExample.m