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);
}
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
resultarray lots of times, you should create it with enough spots to hold the maximum number of strings (you know that number is less thantxt.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[] buffof lengthtxt.Lengthand an index variablejand dobuff[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:
Then you can just use this array to test each character of string in constant time.