473,387 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

string manipulations


Hello ,
I need to perform the following operation:
There are two strings (str1 and strSet) ,I need to write a function
which will get str1 and strSet and will return an index of the first
occurrence of a character in str1 that belongs to the set of characters
in strSet.

I need to perform it with complexity of O(m+n) /n-str lenght ,m-strSet
lenght.

For example: str="fsaghj" strSet="bya" the output will be 3 (the index
of "a" in str).
How can I do it?

Thanks a lot:)

*** Sent via Developersdex http://www.developersdex.com ***
Jan 28 '07 #1
5 1509
csharpula csharp wrote:
I need to perform the following operation:
There are two strings (str1 and strSet) ,I need to write a function
which will get str1 and strSet and will return an index of the first
occurrence of a character in str1 that belongs to the set of characters
in strSet.

I need to perform it with complexity of O(m+n) /n-str lenght ,m-strSet
lenght.

For example: str="fsaghj" strSet="bya" the output will be 3 (the index
of "a" in str).
That sounds like a homework question.
How can I do it?
Well, a simple-minded nested loop, iterating over str then strSet, would
have complexity of O(m*n), so obviously that's out. However, if you
added the characters in strSet to a structure that had O(1) lookup
behaviour, such as a trie or hash, then you'd be able to do the adding
in O(m) and the main loop through str in O(n) for a total of O(m+n).

-- Barry

--
http://barrkel.blogspot.com/
Jan 29 '07 #2

"csharpula csharp" <cs*******@yahoo.comwrote in message
news:e5**************@TK2MSFTNGP05.phx.gbl...
>
Hello ,
I need to perform the following operation:
There are two strings (str1 and strSet) ,I need to write a function
which will get str1 and strSet and will return an index of the first
occurrence of a character in str1 that belongs to the set of characters
in strSet.

I need to perform it with complexity of O(m+n) /n-str lenght ,m-strSet
lenght.

For example: str="fsaghj" strSet="bya" the output will be 3 (the index
of "a" in str).
How can I do it?

Thanks a lot:)
Be sure to acknowledge us when you turn in your homework...
Jan 29 '07 #3
"Barry Kelly" <ba***********@gmail.comschrieb im Newsbeitrag
news:4a********************************@4ax.com...
csharpula csharp wrote:
>I need to perform the following operation:
There are two strings (str1 and strSet) ,I need to write a function
which will get str1 and strSet and will return an index of the first
occurrence of a character in str1 that belongs to the set of characters
in strSet.

I need to perform it with complexity of O(m+n) /n-str lenght ,m-strSet
lenght.

For example: str="fsaghj" strSet="bya" the output will be 3 (the index
of "a" in str).

That sounds like a homework question.
>How can I do it?

Well, a simple-minded nested loop, iterating over str then strSet, would
have complexity of O(m*n), so obviously that's out. However, if you
added the characters in strSet to a structure that had O(1) lookup
behaviour, such as a trie or hash, then you'd be able to do the adding
in O(m) and the main loop through str in O(n) for a total of O(m+n).
wouldn't that have complexity O(log(m)(m+n)) ?

Christof
Jan 29 '07 #4
In .NET I would use the string function 'IndexOfAny'. However the doc
doesn't say what is its complexity.

/LM

"Barry Kelly" <ba***********@gmail.comwrote in message
news:4a********************************@4ax.com...
csharpula csharp wrote:
>I need to perform the following operation:
There are two strings (str1 and strSet) ,I need to write a function
which will get str1 and strSet and will return an index of the first
occurrence of a character in str1 that belongs to the set of characters
in strSet.

I need to perform it with complexity of O(m+n) /n-str lenght ,m-strSet
lenght.

For example: str="fsaghj" strSet="bya" the output will be 3 (the index
of "a" in str).

That sounds like a homework question.
>How can I do it?

Well, a simple-minded nested loop, iterating over str then strSet, would
have complexity of O(m*n), so obviously that's out. However, if you
added the characters in strSet to a structure that had O(1) lookup
behaviour, such as a trie or hash, then you'd be able to do the adding
in O(m) and the main loop through str in O(n) for a total of O(m+n).

-- Barry

--
http://barrkel.blogspot.com/

Jan 29 '07 #5
Christof Nordiek wrote:
Well, a simple-minded nested loop, iterating over str then strSet, would
have complexity of O(m*n), so obviously that's out. However, if you
added the characters in strSet to a structure that had O(1) lookup
behaviour, such as a trie or hash, then you'd be able to do the adding
in O(m) and the main loop through str in O(n) for a total of O(m+n).

wouldn't that have complexity O(log(m)(m+n)) ?
For a hash, insertion is constant time. I mentioned trie but since
lookup in this case is always a single character, it would only be
reasonable to use a single array, rather than a tree, so again it would
be constant time.

-- Barry

--
http://barrkel.blogspot.com/
Jan 29 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zach Tong | last post by:
I recently ran our code through a profiler to determine why it was using so much memory. It turns out the System.String object is taking 95% of the memory. We have considered converting the...
15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
8
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I...
2
by: Faraz | last post by:
Hi, I am working with DOM and I need to do the following: <sequence> <element name="input1" type="string"/> <element name="input2" type="string"/> <element name="input3" type="string"/>...
2
by: news.microsoft.com | last post by:
What is the best way to avoid string manipulations with SQL? I have edit box control where database is opened for attacks through SQL commands. Something like this: selectString = "SELECT...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
12
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
9
by: ma | last post by:
Hello, I want to change one character in a string to a new character. How can i do this. For example I have a string called x and it contains "This is a test" and I want to change the i in is to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.