Check IF one String contains the same characters as another string

1.2k Views Asked by At

I am trying to write a function which will allow me to determine whether one NSString* contains the characters of another NSString*. As an example, refer to the below scenario:

NSString *s1 = @"going";
NSString *s2 = @"ievngcogdl";

So essentially when the comparison between these 2 strings occurs, it should return true as the first string s1 has the same characters of the second string s2. Could I use an NSCountedSet? I know that this class has a method containsObject:(id) although I don't think that will solve my problem. Is there any other ways in completing this function and provide me the required results?

4

There are 4 best solutions below

8
On BEST ANSWER

I think this method could be rather slow, but I would still favour it over [NSString rangeOfCharacterFromSet:], which requires creating an NSCharacterSet object per comparison:

- (BOOL)string:(NSString *)string containsAllCharactersInString:(NSString *)charString {
    NSUInteger stringLen = [string length];
    NSUInteger charStringLen = [charString length];
    for (NSUInteger i = 0; i < charStringLen; i++) {
        unichar c = [charString characterAtIndex:i];
        BOOL found = NO;
        for (NSUInteger j = 0; j < stringLen && !found; j++)
            found = [string characterAtIndex:j] == c;
        if (!found)
            return NO;
    }
    return YES;
}
4
On

Regular Expressions are the best way to check this type of conditions and check this link once

Below I am adding the code for your solution, please check once

  NSString *s1 = @"going"
  NSString *s2 = @"ievngcogdl";

  if ([self string:s1 containsSameCharacterofString:s2]) {

            NSLog(@"YES");

  }


    - (BOOL)string:(NSString *)str containsSameCharacterofString:(NSString *)charString
    {

        if (charString.length >= str.length) {

            NSError *error = nil;
            NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^[%@]+$", charString] options:NSRegularExpressionCaseInsensitive error:&error];

            NSRange textRange = NSMakeRange(0, str.length);
            NSRange matchRange = [regex rangeOfFirstMatchInString:str options:NSMatchingReportProgress range:textRange];

            return (matchRange.location != NSNotFound);

        }
        else {

            return NO;
        }

    }
9
On

This will work -

-(BOOL) string:(NSString *)string1 containsInputString:(NSString *)string2 {

    // Build a set of characters in the string

    NSCountedSet *string1Set = [[NSCountedSet alloc]init];

    [string1 enumerateSubstringsInRange:NSMakeRange(0, string1.length)
                                options:NSStringEnumerationByComposedCharacterSequences
                             usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                 [string1Set addObject:substring];
                             }];


    // Now iterated over string 2, removing characters from the counted set as we go
    for (int i=0;i<string2.length;i++) {
        NSRange range = [string2 rangeOfComposedCharacterSequenceAtIndex:i];
        NSString *substring = [string2 substringWithRange:range];
        if ([string1Set countForObject:substring]> 0) {
            [string1Set removeObject:substring];
        }
        else {
            return NO;
        }
    }
    return YES;
}
5
On
BOOL containsString = [@"Hello" containsString:@"llo"];
if (containsString) {
    // Do Stuff
}