![]() |
efficient way to check the duplicate characters from string2 in string1
Is there any other efficient manner of checking duplicate characters from string2 in string1
Code:
char st1[6]="mikem";Is ther Any other more efficient way , rather than this one -thank you |
Re: efficient way to check the duplicate characters from string2 in string1
Check out strcspn.
|
Re: efficient way to check the duplicate characters from string2 in string1
The algorithm you've presented operates in a time proportional to the lengths of the strings multiplied together, which might be O(n^2) (I don't know a lot about O() representation so that could be way off).
What you could do to improve matters is to run through the first string storing the number of times a particular character has occurred in a 26-number array (storing either the count or just whether or not a letter has been encountered, whichever is more relevant). Then just loop through the second string checking the array if that letter has been encountered. Code:
int i,flags[26];Output of the above is "ol". |
Re: efficient way to check the duplicate characters from string2 in string1
Very nice! That would be hard to beat.
Using tolower() will deal with uppercase letters as well. Code:
int i, n, flags[26]; |
Re: efficient way to check the duplicate characters from string2 in string1
thank you everyone
yeah this definitely has less less complexity, than the previos..which I posted |
| All times are GMT +5.5. The time now is 11:03. |