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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:06:12+00:00 2026-05-28T18:06:12+00:00

I am doing a home work question to split a string without using the

  • 0

I am doing a home work question to split a string without using the framework method.

Following is the working code I came up with.

I would like to know how can I improve the running time to O(n)?

Also any suggestions on improvement are welcome.

public static string[] split(string txt, char[] delim)
{
    char[] text = txt.ToCharArray();
    string[] result = new string[0];
    int count = 0;
    int i = 0;
    StringBuilder buff = new StringBuilder(); 
    while(i < text.Length)
    {
        bool found = false;
        foreach(char del in delim)
        {
            if(del == txt[i])
            {
                found = true;
                break;
            }
        }
        if(found)
        {
            count++;
            Array.Resize(ref result, count);
            result[count - 1] = buff.ToString();
            buff = new StringBuilder();                 
        }
        else
        {
            buff.Append(txt[i]);
        }

        i++;
    }

    if(buff.Length != 0)
    {
        count++;
        Array.Resize(ref result, count);
        result[count - 1] = buff.ToString();
    }

    return(result);
}
  • 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-28T18:06:12+00:00Added an answer on May 28, 2026 at 6:06 pm

    I have a few changes that will simultaneously make this function more C like and reduce the run time to O(n):

    1) Instead of dynamically resizing your result array lots of times, you should create it with enough spots to hold the maximum number of strings (you know that number is less than txt.Length) and then resize it just once at the very end before returning it.

    2) Instead of assembling your results with a StringBuilder, make a char[] buff of length txt.Length and an index variable j and do buff[j++] = txt[i].

    I think your function should be O(N) after you do that. Well technically it will be O(N*M) where M is the number of delimiters.

    EDIT 1:

    Here is a change that will make it be O(N)+O(M) instead of O(N*M):

    Instead of looping through the delimiters for each character in the string, you should loop through the delimiters in ADVANCE and set up an array like this:

    bool[] isDelimiter = new bool[128];  // increase size if you are allowing non-ascii
    foreach(char delim in isDelimiter)
    {
        isDelimiter[(int)char] = true;
    }
    

    Then you can just use this array to test each character of string in constant time.

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

Sidebar

Related Questions

While using Vim (at home and at work), I often find myself doing similar
I'm doing some JAVA coding at home and at work. At home i have
I've been working on a home project using CodeIgniter Reactor, and have come to
I have the following code: # Initialisations filename='/home/Admin/Desktop/Ncapa/i_capa.txt' f = open(filename, 'r') ty =
I am doing homework in Matlab, calculating numeric integration using different methods like simpson,
I'm doing a homework project that requires this: Below you will find the code
This is NOT a homework question, actually I am doing this for fun. Here
I was doing some j2mee work on ubuntu linux with Netbeans 6.7.1. I went
Given a web application built using Jboss seam with following details: dynamic web module:
This is a homework question, so I would like to avoid complete answers and

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.