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 *** 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/
"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...
"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
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/
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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"/>...
|
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...
|
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...
|
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...
|
by: Tee |
last post by:
String Builder & String, what's the difference.
and when to use which ?
Thanks.
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |