Frage im Vorstellungsgespräch bei Meta

Write a palindrome-checking function

Antworten zu Vorstellungsgespräch

Anonym

19. Nov. 2012

This has a N/2 complexity : function isPalyndrome($str) { $array = str_split($str); $size = count($array); $pivot = floor($size / 2); for($i = 0; $i < $pivot; $i++) { if($array[$i] != $array[$size - $i - 1]) { return false; } } return true; }

2

Anonym

29. Dez. 2012

#include #include using namespace std; bool IsPalindrome(const string& input) { for (int i = 0; i < input.size()/2; ++i) { if (input[i] != input[input.size()-i-1]) { return false; } } return true; } int main() { const string str("madam"); const string str1("madama"); cout << IsPalindrome(str) << endl; cout << IsPalindrome(str1) << endl; return 0; }

Anonym

15. Nov. 2012

My solution walked char pointers back from the end and forward from the beginning of the string, returning true if the crossed over and false if the two chars pointed to didn't match.