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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:11:49+00:00 2026-05-29T18:11:49+00:00

I am trying to check the time complexity of the below simple program. The

  • 0

I am trying to check the time complexity of the below simple program.
The program replaces spaces in a string with '%20'.

  1. The loop to count spaces (O(1) time)

        foreach (char k in s)
        {
            if (k == ' ')
            {
                spaces_cnt++;
            }
        }
    
  2. The loop to replace the spaces (O(n) where n is size of string)

        char[] c = new char[s.Length + spaces_cnt * 3];
        int i = 0;
        int j = 0;
        while (i<s.Length)
        {
            if (s[i] != ' ')
            {
                c[j] = s[i];
                j++;
                i++;
            }
            else
            {
    
                c[j] = '%';
                c[j + 1] = '2';
                c[j + 2] = '0';
                j = j + 3;
                i++;
            }
        }
    

So I am guessing it is a “O(n) + O(1)” solution. Please correct me if I am wrong.

  • 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-05-29T18:11:51+00:00Added an answer on May 29, 2026 at 6:11 pm

    The loop to count spaces takes O(n), not O(1), since you’re iterating over – and performing a check on – each of the n characters in your string.

    As you stated, the replacement loop takes O(n). Two O(n) operations performed sequentially have a combined complexity of O(n) (constant factors are discarded in Big-O notation).

    P.S. You know that you can achieve the equivalent of all your code using a single line?

    s = s.Replace(" ", "%20");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Check this link! I am having the hardest time trying to figure out why
Why linq is trying to check second expression anyway? .Where(t => String.IsNullOrEmpty(someNullString) || t.SomeProperty
I've been trying to use check_time.vbs to check the Windows time. Here's the script:
I am trying to check an entered time in a DateTimePicker control and correct
I'm trying to check for variable changes within a specified amount of time. For
I have this method, isPalindrome(), and I am trying to find the time complexity
I have a really simple program where I'm trying to add a directory to
I am trying to check how much time does a function takes to execute
I'm trying to check a string passed through the URL and get back all
In my Android Application , I am trying to convert Date/Time to Milliseconds, check

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.