Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8744019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:46:38+00:00 2026-06-13T11:46:38+00:00

I have an NSString called query which contains ~10 characters. I would like to

  • 0

I have an NSString called query which contains ~10 characters.

I would like to check to see if a second NSString called word contains all of the characters in query, or some characters, but no other characters which aren’t specified in query.

Also, if there is only one occurrence of the character in the query, there can only be one occurrence of the character in the word.

Please could you tell me how to do this?

NSString *query = @"ABCDEFJAKSUSHFKLAFIE";
NSString *word  = @"fearing"; //would pass as NO as there is no 'n' in the query var.  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:46:39+00:00Added an answer on June 13, 2026 at 11:46 am

    The following answers the first half:

    NSCharacterSet *nonQueryChars = [[NSCharacterSet characterSetWithCharactersInString:[query lowercaseString]] invertedSet];
    NSRange badCharRange = [[word lowercaseString] rangeOfCharacterFromSet:nonQueryChars];
    if (badCharRange.location == NSNotFound) {
        // word only has characters in query
    } else {
        // found unwanted characters in word
    }
    

    I need to think about the second half of the requirement.

    Ok, the following code should fulfill both requirements:

    - (NSCountedSet *)wordLetters:(NSString *)text {
        NSCountedSet *res = [NSCountedSet set];
    
        for (NSUInteger i = 0; i < text.length; i++) {
            [res addObject:[text substringWithRange:NSMakeRange(i, 1)]];
        }
    
        return res;
    }
    
    - (void)checkWordAgainstQuery {
        NSString *query = @"ABCDEFJAKSUSHFKLAFIE";
        NSString *word  = @"fearing";
    
        NSCountedSet *queryLetters = [self wordLetters:[query lowercaseString]];
        NSCountedSet *wordLetters = [self wordLetters:[word lowercaseString]];
        BOOL ok = YES;
        for (NSString *wordLetter in wordLetters) {
            int wordCount = [wordLetters countForObject:wordLetter];
            // queryCount will be 0 if this word letter isn't in query
            int queryCount = [queryLetters countForObject:wordLetter];
            if (wordCount > queryCount) {
               ok = NO;
               break;
            }
        }
    
        if (ok) {
            // word matches against query
        } else {
            // word has extra letter or too many of a matching letter
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Shop that contains data members ( NSString , NSInteger
I have an NSString called animation, which is called with the following (working) code:
I have an NSMutableArray called myObjectArray which contains and array of NSObjects called myObject.
I have a subclassed UITableViewCell called Comment, which has a UILabel containing some relevant
I have NSString like My name is. I want to remove text after coming
I have: NSString *promise = @thereAreOtherWorldsThanThese; which I'm trying to transform into the string:
I have a NSString object having multiple conditions specified in it like below: NSString
I have an NSString that I'd like to append a { at the beginning
I have an NSString declared like this : .h: @interface ViewController : UIViewController {
I have a form(table view) which contains textfields and textview as subviews,I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.