Cracking the coding interview | Step-11 |encoding three strings

Question 1:

Identify possible words: Detective Bakshi while solving a case stumbled upon a letter which had many words whose one character was missing i.e. one character in the word was replaced by an underscore. For e.g.“Fi_er”. He also found thin strips of paper which had a group of words separated by colons, for e.g. “Fever:filer:Filter:Fixer:fiber:fibre:tailor:offer”. He could figure out that the word whose one character was missing was one of the possible words from the thin strips of paper. Detective Bakshi has approached you (a computer programmer) asking for help in identifying the possible words for each incomplete word.

You are expected to write a function to identify the set of possible words.
The function  identifyPossibleWords takes two strings as input
where, 

input1 contains the incomplete word, and
input2 is the string containing a set of words separated by colons.

The function is expected to find all the possible words from input2 that can replace the incomplete word input1, and return the result in the format suggested below.

Example1 -

input1 = “Fi_er”
input2 = “Fever:filer:Filter:Fixer:fiber:fibre:tailor:offer”

output string should be returned as “FILER:FIXER:FIBER”
Note that –

The output string should contain the set of all possible words that can replace the incomplete word in input1
all words in the output string should be stored in UPPER-CASE
all words in the output string should appear in the order in which they appeared in input2, i.e. in the above example we have FILER followed by FIXER followed by FIBER.
While searching for input1 in input2, the case of the letters are ignored, i.e “Fi_er” matches with “filer” as well as “Fixer” as well as “fiber”.
IMPORTANT: If none of the words in input2 are possible candidates to replace input1, the output string should contain the string “ERROR-009”

Assumption(s):

Input1 will contain only a single word with only 1 character replaced by an underscore “_”
Input2 will contain a series of words separated by colons and NO space character in between
Input2 will NOT contain any other special character other than underscore and alphabetic characters.

import java.io.*;
import java.util.*;
class UserMaincode
{
   public String indentifyPossibleWords(String input1, String input2)
{
String st[]=input2.split(":");
        String str="";
        for(int i=0;i<st.length;i++)
        {
            if(st[i].length()!=input1.length())
                continue;
            String test=input1;
            int x=test.indexOf("_");
            char ch=st[i].charAt(x);
            test=test.replace('_', ch);
            test=test.toUpperCase();
            st[i]=st[i].toUpperCase();
            if(st[i].equals(test))
            {
                if(str=="")
                {
                    str=str+test;
                }
                else
                {
                    str=str+":"+test;
                }
            }
        }
        if(str=="")
            str="ERROR-009";
        return str;
}
}











Question 2:

Encoding Three Strings: Anand was assigned the task of coming up with an encoding mechanism for any given three strings. He has come up with the below plan.
STEP ONE: Given any three strings, break each string into 3 parts each.
For example – If the three strings are as below -
Input1= “John”
Input2= “Johny”
Input3= “Janardhan”
“John” should be split into “J”, “oh”, “n” as the FRONT, MIDDLE and END parts respectively.
 “Johny” should be split into “Jo”, “h”, “ny” as the FRONT, MIDDLE and END parts respectively.
“Janardhan” should be split into “Jan”, “ard”, “han” as the FRONT, MIDDLE and END parts respectively.
i.e. if the no. of characters in the string are in multiples of 3, then each split-part will contain equal no. of characters, as seen in the example of “Janardhan”
If the no. of characters in the string are NOT in multiples of 3, and if there is one character more than multiple of 3, then the middle part will get the extra character, as seen in the example of “John”
If the no. of characters in the string are NOT in multiples of 3, and if there are two characters more than multiple of 3, then the FRONT and END parts will get one extra character each, as seen in the example of “Johny”
STEP TWO:  Concatenate (join) the FRONT, MIDDLE and END parts of the strings as per the below specified concatenation-rule to form three Output strings.
Output1 = FRONT part of Input1 + FRONT part of Input2 + FRONT part of Input3
Output2 = MIDDLE part of Input1 + MIDDLE part of Input2 + MIDDLE part of Input3
Output3 = END part of Input1 + END part of Input2 + END part of Input3
For example, for the above specified example input strings,
Output1 = “J” + “Jo” + “Jan” = “JJoJan”
Output2 = “oh” + “h” + “ard” = “ohhard”
Output3 = “n” + “ny” + “han” = “nnyhan”
Step THREE: After the first two steps, we will have three output strings. Further processing is required only for the third output string as per below rule –
“ Toggle the case of each character in the string”, i.e. in the third output string, all lower-case characters should be made upper-case and vice versa.
For example, for the above example strings, Output3 is “nnyhan”, so after applying the toggle rule, Output3 should become “NNYHAN”.
Final Result – The three output strings after applying the above three steps is the final result. i.e. for the above example,
Output1 = “JJoJan”
Output2 = “ohhard”
Output3 = “NNYHAN”
Anand approaches you to help him write a program that would do the above mentioned processing on any given three strings and generate the resulting three output strings
Note that the three output strings should be returned as members of a "Result" object/struct.



String frnt1="",mid1="",end1="";
  String frnt2="",mid2="",end2="";
  String frnt3="",mid3="",end3="";
  String output1="",output2="",output3="";
  int len1=input1.length();
  int len2=input2.length();
        int len3=input3.length();
  if(len1==input1.length()){
  if(len1%3==0)
  {
   frnt1=input1.substring(0, (len1/3));
   mid1=input1.substring((len1/3), (2*(len1/3)));
   end1=input1.substring(2*(len1/3));
  }
  else if((len1-1)%3==0)
  {
   frnt1=input1.substring(0, (len1/3));
   mid1=input1.substring((len1/3), ((2*(len1/3))+1));
   end1=input1.substring(((2*(len1/3))+1));
  }
  else if((len1-2)%3==0)
  {
   frnt1=input1.substring(0, ((len1/3)+1));
   mid1=input1.substring(((len1/3)+1), ((2*(len1/3))+1));
   end1=input1.substring(((2*(len1/3))+1));
  }
  }
   if(len2==input2.length()){
  if(len2%3==0)
  {
   frnt2=input2.substring(0, (len2/3));
   mid2=input2.substring((len2/3), (2*(len2/3)));
   end2=input2.substring(2*(len2/3));
  }
  else if((len2-1)%3==0)
  {
   frnt2=input2.substring(0, (len2/3));
   mid2=input2.substring((len2/3), ((2*(len2/3))+1));
   end2=input2.substring(((2*(len2/3))+1));
  }
  else if((len2-2)%3==0)
  {
   frnt2=input2.substring(0, ((len2/3)+1));
   mid2=input2.substring(((len2/3)+1), ((2*(len2/3))+1));
   end2=input2.substring(((2*(len2/3))+1));
  }
  }
   if(len3==input3.length()){
  if(len3%3==0)
  {
   frnt3=input3.substring(0, (len3/3));
   mid3=input3.substring((len3/3), (2*(len3/3)));
   end3=input3.substring(2*(len3/3));
  }
  else if((len3-1)%3==0)
  {
   frnt3=input3.substring(0, (len3/3));
   mid3=input3.substring((len3/3), ((2*(len3/3))+1));
   end3=input3.substring(((2*(len3/3))+1));
  }
  else if((len3-2)%3==0)
  {
   frnt3=input3.substring(0, ((len3/3)+1));
   mid3=input3.substring(((len3/3)+1), ((2*(len3/3))+1));
   end3=input3.substring(((2*(len3/3))+1));
  }
  }
  output1=frnt1+frnt2+frnt3;
  output2=mid1+mid2+mid3;
  output3=end1+end2+end3;
  System.out.println(output3);
  output3=changeCase(output3);
  Result rs=new Result(output1,output2,output3);
  return rs;
    }
 public static String changeCase(String str)
 {
  StringBuffer newS = new StringBuffer(str);
  for(int i=0;i<str.length();i++)
  {
   Character c=str.charAt(i);
   if(Character.isLowerCase(c))
   {
    newS.replace(i, i+1, Character.toUpperCase(c)+"");
   }
   else
   {
    newS.replace(i, i+1, Character.toLowerCase(c)+"");
   }
  }
  str=newS.toString();
  return str;
 }

Post a Comment

1 Comments

  1. In Question 1
    Fi_er
    Fever:filer:Filter:Fixer:fiber:fibre

    if the above input is given it is working fine

    when in input they are 2 _ _ "blanks" is is printing "ERROR-009"
    Example :-
    Fi_e_
    Fever:filer:Filter:Fixer:fiber:fibre

    output is "ERROR-009"
    Where as the output should be :
    FILER:FIXER:FIBER

    ReplyDelete

Please do not Enter any spam link in the comment box