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.98 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.97 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.96 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.95 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.94 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.93 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.92 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.91 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.90 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.89 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.88 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.87 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.86 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.85 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.84 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.83 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.82 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.81 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.80 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.79 tracks/objective-c/exercises/bracket-push/BracketPushExample.m