Pergunta de entrevista da empresa Amazon

Find the max ;length palindrome in an input string.

Respostas da entrevista

Sigiloso

13 de mar. de 2011

A stupid and slow solution (O(n^3)): public static string MaxPalindrom(string str) { string maxP = String.Empty; for (int i = 0; i maxP.Length) { maxP = str.Substring(i, j - i); } } } return maxP; } private static bool isP(string str) { for (int i = 0, j = str.Length - 1; i < j; i++, j--) { if (str[i] != str[j]) { return false; } } return true; }

Sigiloso

27 de mar. de 2011

Here's a code with O(n2) complexity #include #include #include void ispal(char*,int,int*); void longpal(char *str) { int longest[2]={0}; int i=0; for(;i=0&&j(longest[1]-longest[0])) { longest[0]=i; longest[1]=j; } } i--; j++; } } void main() { char *str; scanf("%s",str); longpal(str); getch(); }

Sigiloso

27 de mar. de 2011

My answer is based on http://stevekrenzel.com/articles/longest-palnidrome translated to c language.