Pages

Wednesday 22 August 2012

Yahoo coding problem 2: 2012

Yahoo coding problem 2: 2012

finding common prefix in array of strings
  
testcase1: "india"
                 "indka" 
                  "indus"
                  "indaa"

output:ind 
n strings and n length
timecomplexity :nlogn

solution:

#include<stdio.h>
#include<math.h>
int main()
{
    char a[4][6]={"india" "indka" "indus","indaa"};
        int i,j,k=0,l=0,p=3,m;
      for(m=0;m<log2(4);m++)
      {
          for(i=0;i<=p;)
            {
                for(j=0;j<5;)
                {
                    if(a[i][j]==a[i+1][j])
                    {              
                        a[k][l]=a[i][j];
                        l++;
                        j++;
                     }
                    else  j=5;
                }
                a[k][l]='\0';
                k++;
                i=i+2;
             }
            p=p/2;
            k=0;
        }
        printf("%s",a[0]);
      }

No comments: