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 8442115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:45:41+00:00 2026-06-10T08:45:41+00:00

The following code returns the first location in the string s1 where any character

  • 0

The following code returns the first location in the string s1 where any character from the string s2 occurs. Its worst time complexity is O(m+n). How?

#include<stdio.h> 

int any(char *s1, char *s2) 
{

    char array[256]; 

    int  i;
    if (s1 == NULL) {
        if (s2 == NULL) {
            return(0);
        } else {
            return(-1);
        }
    }

    for(i = 0; i < 256; i++) {
        array[i] = 0;
    }

    while(*s2 != '\0') {
        array[*s2] = 1;
        s2++;
    }

    i = 0;
    while(s1[i] != '\0') {
        if (array[s1[i]] == 1) {
            return(i);
        }
        i++;
    }
    return(-1);
}
  • 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-10T08:45:43+00:00Added an answer on June 10, 2026 at 8:45 am

    It does this in two steps.

    1. It initializes an array of size 256 (representing each of the valid characters for its input strings), and for each letter in s2 (n) marks that characters spot in the array as 1 to indicate that that character is present.

    2. It iterates across the characters in s1 (0 up to m), checking each characters position in the array to see if it is set to “present” (1), which would indicate that it is in the second string. If it is, it returns the index of that character in s1. If none of the characters in s1 are present in s2 (discovered at m), it returns -1.

    Since step 1 will always take n (length of s2), and step 2 will take up to m (the length of s1), the worst case is O(m+n), which occurs only when either there are no matches, or only the last character in s1 is present in s2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code returns the error from the For Each loop. I have similar
The following code returns an error and I dont know why: require rexml/document include
The following code returns me people with similar telephone numbers. It works perfectly but
I the following code returns every text that has the price class associated to
ms-access is connecting to a local mysql database the following code returns an error:
Forgive me, I'm a beginner. The following code returns a parse error: $query =
The following C code returns a segmentation fault error. I do not understand why
I have the following code that returns a .CSV file when a link is
I have the following code that returns HTTP error 400 everytime I try to
I have multiple messages in SQS. The following code always returns only one, even

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.