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.159 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.158 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.157 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.156 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.155 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.154 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.153 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.152 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.151 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.150 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.149 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.148 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.147 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.146 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.145 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.144 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.143 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.142 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.141 tracks/objective-c/exercises/bracket-push/BracketPushExample.m
trackler-2.2.1.140 tracks/objective-c/exercises/bracket-push/BracketPushExample.m