tashfeen_suleman
Check me out on the web at http://na/.
Recent College Grad. BS in Computer Science
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Gary Daniels and Evan Goldring - Mock whiteboard problem
Aug 25, 2004 at 4:42 PMI would tackle the palindrome problem in this manner. Opinions on the code and complexity are welcome
:
Input:
Characters, Numbers, Alphanumerics, White Spaces, Wildcards.
----
Example Test Cases:
tarat, ta r at, tar at, ta 1 at, ab 1A1 ba, ab1A 1ba etc (The code considers them correct inputs).
Ta a, ta 1 ata, etc are not palindromes.
----
Output:
True or False
----
Algorithm:
To acheive efficient memory management, computation time and code complexity, I would parse through the text from the two
corners ie begining and end and reach the center of the text while comparing each of the input characters. There would be
checks for white spaces. Other characters such as commas, appostrophies etc would be considered as character inputs. If it
encounters white spaces, it would ignore them and (increment the begining counter or decrement the end counter).
Other implementation such as a recursive one would require extra stack space and recursive calls to the heap. Which might
not be a good idea. Reversing the string and comparing with the original one would also not be a good idea as extra memory
would be required.
Pseudocode:
-Initilize all variables ie int beg, end, bool check variables.
-point the beg pointer to the first character of the input string.
-Find length of string and point the 'end pointer' to the (str_len - 1)th position.
-Enter a while loop and compare the beg and end.
---If they are equal, do beg++ and end--.
---if they are not, check for white spaces, ignore white spaces, and inc or dec accordingly ie if beg, inc beg and leave end
as it is, OR, if it is end, than dec end and leave beg to point to where it is pointing.
----if they are still not equal, return FALSE.
-end of program.
----
Complexity:
The complexity of the code would be O(n) where n is the length of the input string. In reality, the code would run for n/2
iterations as when the beg pointer reaches the middle+1th part of the string, it would stop the while loop.
Code:
#include <iostream.h>
#include <string.h>
bool ispalin(char * input)
{
char * beg=new char;
char * end = new char;
beg = input;
end = &input[strlen(input)-1];
if(beg==0 || beg==NULL)
return false;
while(*beg==*end && *beg != input[strlen(input)/2])
{
if(*beg == ' ')
beg++;
if(*end = ' ')
end--;
if(beg!=end)
return false;
if(beg==end)
return true;
beg++;
end--;
}
}
int main()
{
bool check=false;
char * input = new char[30];
cin.getline(input, 30, '\n');//as cin.getline caters to white spaces as well
check = ispalin(input);
if(check==0)
{
cout<<"equal\n";
}
else
cout<<"not equal\n";
return 0;
}
-------