Re: Microsoft Interview Questions
- From: Bryan Olson <fakeaddress@xxxxxxxxxxx>
- Date: Sun, 21 Jan 2007 16:34:11 -0800
Patricia Shanahan wrote:
stdazi@xxxxxxxxx wrote:Q2) What is an efficient way to determine whether a string is a
permutation of another string? What is your run time?
damn i forgot.. another method would be to use XOR and apply it to some
integer for each char of the two strings (presuming that the char value
can be casted to an integer tipe...) like that :
4 int isperm(char *s1, char *s2) {
5
6 int retval = 0;
7
8 while (*s1 || *s2) {
9 retval ^= *s1 ^ *s2 ;
10 ++s1; ++s2;
11 }
12 return retval || (*s1 != *s2);
13 }
this is a O(n) algorithm where n is the lenght of the "shortest" string.
The code suggests that you are assuming zero-terminated strings. Under
that assumption, it appears to me to test two conditions:
Right, and it also exhibits undefined behavior, because the while
condition uses a logical OR where he probably wanted AND.
--
--Bryan
.
- References:
- Microsoft Interview Questions
- From: kool_guy
- Re: Microsoft Interview Questions
- From: stdazi@xxxxxxxxx
- Re: Microsoft Interview Questions
- From: Patricia Shanahan
- Microsoft Interview Questions
- Prev by Date: Re: Graph Coloring
- Next by Date: Re: Microsoft Interview Questions
- Previous by thread: Re: Microsoft Interview Questions
- Next by thread: Re: Microsoft Interview Questions
- Index(es):
Relevant Pages
|