link mingle home | logged in as: guest | login/register| submit link


IndiaDiscuss.com : Social Bookmarkings and News Networking Site for India
interview_questions
Bookmarks
Find String in a box
21
Votes

You have a 2-D input array of characters. You need to find whether a given string can be found in the array . Write a program to do it
Examples:
1. search 'computer' in 
              {'c','o','m','p','u'},
              {'c','o','m','t','u'},
              {'c','o','e','e','u'},
              {'c','r','m','f','u'},
              {'c','o','m','p','u'},
              {'c','o','m','p','u'},

   returns TRUE;

2. 1. search 'computer' in 
              {'c','o','m','p','u'},
              {'c','o','m','t','u'},
              {'c','o','e','e','u'},
              {'c','o','m','f','u'},
              {'c','o','m','p','u'},
              {'r','o','m','p','u'},

   returns FALSE;


saved under Microsoft Interview Questions by interview_questions

 
   public boolean find(char[][] box, char[] str){
    int R = box.length; int C = box[0].length;
    int[][] dp = new int[R][C];
    int N = str.length;
   
   int max = -1;
   
   for(int k=0;k<N;k++){
    for(int i=0 ;i<R ;i++){
       for(int j=0; j<C ;j++){
           for (int x=-1;x<=1;x++){
               for (int y=-1;y<=1;y++){
                 if(!( (i+x)<R && (i+x>=0) &&
                       (j+y)<C && (j+y>=0)) || 
                       (x==0 && y==0))continue;
                  if( box[i+x][j+y]== str[k] 
                      && dp[i][j]==k ){
                     dp[i+x][j+y] =k+1;
                     max = Math.max(max,k+1);
                  }
               }
           }
         }
       }
    }
    if(max == str.length)return true;
    return false;
   }


comment by search on 2008-06-27 04:45:38
 



Enter the string above
 
IndiaDiscuss | Published News | Hot
Indian Social News and Links Network
IndiaDiscuss | Published News
Indian Social News and Links Network