Microsoft Interview Question
Software Engineer in Testswe can start from the left end of the string and gradually move towards right end checking at every step its neighbors(both cases for odd and even length palimdrome) and if criteria for a palindrome if met then spread further in left and right direction to check if they also meet the criteria ,keep speading at each step until criteria fails(then record the start and end index of this palindrome string).. keep doin this till u reach the right end of the string.
hope my logic is convincing for most of the people who have posted the solution or the comments and in case u find any flaw then just let me know ...
reverse the string and then it becomes the longest common subsequence problem b/w (string, reverse string)
can be solved in O(n)
huge mistake, above will not be a solution ... LCS need not find consecutive characters
longest common subsequence is not solvable in O(n) it is solvable in O(n^2) using dynamic pgmg... longest common substring is an easier version of the same problem and can also be solved in O(n^2). However using a suffix tree to find the longest common substring is d best way and solves it in O(n)...
#include <stdio.h>
#include<string.h>
int main()
{
int i,j,a[256],max=0,c=0,w,u,k,p;
char s[30];
printf("enter the string\n");
gets(s);
for(i=0;i<256;i++)
{
a[i]=0;
}
for(i=0;s[i]!='\0';i++)
{
a[s[i]]++;
}
i=0;
while(s[i]!='\0')
{
j=strlen(s)-1;
if(a[s[i]]>1)
{
while(j!=i)
{
if(s[j]==s[i])
{
k=j-1;
p=i+1;
c=0;
while((k>p)&&(s[k]==s[p]))
{
k=k-1;
p=p+1;
c=c+1;
}
if(k<=p)
{
if(c>max)
{
max=c;
w=i;
u=j;
}
a[s[i]]--;
break;
}
}
j--;
}
}
i++;
}
if(max>0){
printf("the required plaindrome is:\t");
while(w<=u)
{
putchar(s[w]);
w++;
}
}
else{
printf("there isnt any palindrome");
}
return 0;
}
there is slight modification:
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,a[256],max=0,c=0,w,u,k,p;
char s[30];
printf("enter the string\n");
gets(s);
for(i=0;i<256;i++)
{
a[i]=0;
}
for(i=0;s[i]!='\0';i++)
{
a[s[i]]++;
}
i=0;
while(s[i]!='\0')
{
j=strlen(s)-1;
if(a[s[i]]>1)
{
while(j!=i)
{
if(s[j]==s[i])
{
k=j-1;
p=i+1;
c=0;
while((k>=p)&&(s[k]==s[p]))
{
k=k-1;
p=p+1;
c=c+1;
}
if(k<=p)
{
if(c>max)
{
max=c;
w=i;
u=j;
}
a[s[i]]--;
break;
}
}
j--;
}
}
i++;
}
if(max>0){
printf("the longest palindrome is:\t");
while(w<=u)
{
putchar(s[w]);
w++;
}
}
else{
printf("there isnt any palindrome");
}
return 0;
}
there is slight modification:
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,a[256],max=0,c=0,w,u,k,p;
char s[30];
printf("enter the string\n");
gets(s);
for(i=0;i<256;i++)
{
a[i]=0;
}
for(i=0;s[i]!='\0';i++)
{
a[s[i]]++;
}
i=0;
while(s[i]!='\0')
{
j=strlen(s)-1;
if(a[s[i]]>1)
{
while(j!=i)
{
if(s[j]==s[i])
{
k=j-1;
p=i+1;
c=0;
while((k>=p)&&(s[k]==s[p]))
{
k=k-1;
p=p+1;
c=c+1;
}
if(k<=p)
{
if(c>max)
{
max=c;
w=i;
u=j;
}
a[s[i]]--;
break;
}
}
j--;
}
}
i++;
}
if(max>0){
printf("the longest palindrome is:\t");
while(w<=u)
{
putchar(s[w]);
w++;
}
}
else{
printf("there isnt any palindrome");
}
return 0;
}
assume every char a possible center of a plindrom save current max length of palindrom seen so far and continue..its a o(n2) soln but for an interview its good enough i blv..
pls let me know if it fails for any csae.
thanks
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,a[256],max=0,c=0,w,u,k,p, mc, l, st, e;
char s[30];
printf("enter the string\n");
gets(s);
mc = 1;
l = strlen(s);
c = 1; st = e = 0;
for(i = 0;i<l;i++)
{
j = i-1;k = i+1; c = 1;
while(1)
{
if(j>=0 && k<l && (s[j] == s[k]))
{
j--;
k++;
c+=2;
continue;
}
if(k<l && (s[i] == s[k]))
{
k++;
c+=1;
continue;
}
else if(mc<c)
{
if(c==2)
st = j;
else
st = j+1;
e = k-1;
mc = c;
}
break;
}
}
if(mc <=1)
printf("there isnt any palindrome");
else
{
printf("%d", mc);
for(;st<=e;st++)
printf("\t%c", s[st]);
}
getchar();
return 0;
}
Build a suffix tree for concatenation of string with its reversal...O(n) time and space. Should be pretty easy from there to check for palindromes. See for example Gusfield's book on algorithms for strings and sequences...lots of stuff on suffix trees for string problems. Good thing to know how to apply, then maybe mumble that the details on building suffix trees are complicated and it's been a while
#include <stdio.h>
char *getLongestPallindrome(char *);
int checkPallindrome(char *);
char *extract(char *,int,int);
void main()
{
char *str="cdeedc";
printf("%s",getLongestPallindrome(str));
}
char *getLongestPallindrome(char *s)
{
char *ostr=(char *)malloc(sizeof(s));
int i=0,j,k=0;
while(s[i]!=NULL)
{
k=i;
j=strlen(s)-1;
while(k<=j)
{
if(s[k]==s[j])
{
if(checkPallindrome(extract(s,k,j)) && strlen(extract(s,k,j))>strlen(ostr))
{
strcpy(ostr,extract(s,k,j));
break;
}
}
//k++;
j--;
}
i++;
}
return ostr;
}
int checkPallindrome(char *str)
{
int flag=0,f=0,l=strlen(str)-1;
while(f<=l)
{
if(str[f]!=str[l])
{
flag=1;
break;
}
f++;
l--;
}
if(flag==1)
return 0;
return 1;
}
char *extract(char *s,int f,int l)
{
int i=0;
char *os=(char *)malloc(sizeof(s));
while(f<=l)
{
os[i++]=s[f++];
}
os[i]=NULL;
return os;
}
Python Code
import logging
def findLargestPallindrome(inputString):
stringLen = len(inputString)
index =0
palindromes= []
longestPallindrome = ''
while stringLen>index:
palLen =1
while (index + (palLen*2))<=stringLen:
splitPart = inputString[index+palLen:index+palLen*2]
if inputString[index:palLen+index] == splitPart[::-1] and (palLen*2)>len(longestPallindrome) :
longestPallindrome =inputString[index:index+palLen*2]
palLen +=1
index +=1
return longestPallindrome
print findLargestPallindrome('ssaaddaassssaaddaass')
1. Declare an array of size 256 (represents 256 ASCII chars) say A
2. Fill the array with 0's initially.
3. Read the given string and update the count in array at the ascii position.
Eg. if 'a' then A[97] = 1. If found again just make A[97] = 2.
4. Read the array A and form the palindrome accordingly.
Time complexity: O(n) - Reading array of size n + O(1) - Reading array of size 256
== O(n)
Space complexity: O(1)
- manu October 02, 2010