Sunday, September 16, 2012

amazon coding challenge interviewstreet

today submitted the modified submission of "Shortest Sub-segment". It took a day to fix the bug in the way I am reading and storing the input...finally got AC :) ....and at the same time my count reached to 4/4.

Happy coding !!

My solution for the "Shortest Sub-segment" is below:

/***************************************************************/
/* author: rs                                                                                                           */
/***************************************************************/
import java.util.*;


class entry{
    String s;
    int v;
}

class entryComparator implements Comparator <entry> {
    public int compare(entry e1, entry e2) {

        int result = 0;
        if(e1.v > e2.v)
        {
             result = 1;
        }
        else if(e1.v < e2.v){
             result = -1;
        }
        return result;
    }
}


public class Solution {

public static HashSet<String> S1;
public static HashSet<String> S2;
public static HashMap<String,Integer> M1;
public static String First;
public static String[] Arr;
public static String[] Arr_p;
public static String[] Arr_k;
public static entry[] K1;
public static int[] K2;
public static int k=0;
public static int min=Integer.MAX_VALUE;
public static int max=Integer.MIN_VALUE;
public static int f_len=Integer.MAX_VALUE;
public static int len=0,end=0,f_end=0;
public static boolean found=false;


 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  First = sc.nextLine();

  First = First.replaceAll("[^a-zA-Z| |]", " ");
  First = First.replaceAll("  ", " ");
  Arr=First.split(" ");
  Arr_p=First.split(" ");
  for(int i=0;i<Arr.length;i++)
      Arr[i]=Arr[i].toLowerCase();
  k = sc.nextInt();
  sc.nextLine();

  String tmp;

  /* Initializing the basic data structure */
  M1=new HashMap<String,Integer> ();
  S1=new HashSet<String> ();
  S2=new HashSet<String> ();
  Arr_k=new String[k];
  K1=new entry[k];
  int fix=0;
  /* reading the k words putting in S1,M1 and array Arr_k*/
    for(int i=0;i<k;i++){
      tmp= sc.nextLine().toLowerCase();
   
      if(!S1.contains(tmp))
      {
        M1.put(tmp,fix);
        S1.add(tmp);
        Arr_k[fix++]=new String(tmp);
      }
   
  }//end of for

  K1=new entry[M1.size()];
  for(int i=0;i<fix;i++)
  {
      K1[i]=new entry();
      if(M1.containsKey(Arr_k[i]))
      K1[i].s=Arr_k[i];
      K1[i].v=-1;
  }
  /* copy s1 to s2 */
  S2.addAll(S1);

   int i;
  for(i=0;i<Arr.length;i++)
  {
      if(S2.contains(Arr[i]))
      {
          S2.remove(Arr[i]);
          if(M1.containsKey(Arr[i]))
          {
              int t_int = M1.get(Arr[i]);
              K1[t_int].v=i;
              if(min > i)
                min = i;
              if(max < i)
               max = i;
          }

      }
       else if(M1.containsKey(Arr[i]))
      {
          int t_int=M1.get(Arr[i]);
          K1[t_int].s=Arr[i];
          K1[t_int].v=i;
          if(min > i)
              min = i;
          if(max < i)
              max = i;
       }

      if(S2.isEmpty())
      {
          end=i;
          len=max-min;
          if(f_len > len)
          {
              f_len=len;
              f_end=max;
              if(len == k-1)
                 found=true;
              break;

          }
      }
  }//end of for
  if(found==true)
  {   for(int j=f_end-f_len;j<=f_end;j++)
      {
          System.out.print(Arr_p[j]+" ");
      }
  }
 else if(i==Arr.length && !S2.isEmpty())
  {
     System.out.println("NO SUBSEGMENT FOUND");
 }
 else // now handling the rest of the array
  {   int m_flag=0;
      Arrays.sort(K1,new entryComparator());
      int counter=max+1;
      K2=new int[K1.length];
      for(int p=0;p<K2.length;p++)
      {
          K2[p]=-1;
      }
      entry temp_entry=new entry();
      temp_entry.s=K1[0].s;
      temp_entry.v=K1[0].v;
      if(f_len > K1[K1.length-1].v- K1[0].v)
      {
              f_len=K1[K1.length-1].v- K1[0].v;
              f_end=K1[K1.length-1].v;
              if(f_len == k-1)
              {
                    m_flag=1;
              }


      }
      while(counter < Arr.length && m_flag==0)
      {

          if(Arr[counter].equalsIgnoreCase(temp_entry.s) )//&& M1.containsKey(Arr[counter]))
          {
               K2[M1.get(K1[0].s)]=counter;

               for(int t=0;t<K1.length;t++)
               {
                   if(K2[M1.get(K1[t].s)]!=-1)
                   {
                        K1[t].v=K2[M1.get(K1[t].s)];
                   }
               }
               for(int t=0;t<K2.length;t++)
               {
                    K2[t]=-1;
               }
              Arrays.sort(K1,new entryComparator());

              temp_entry.s=new String(K1[0].s);
              temp_entry.v=K1[0].v;
              if(f_len > K1[K1.length-1].v- K1[0].v)
              {
                    f_len=K1[K1.length-1].v- K1[0].v;
                    f_end=K1[K1.length-1].v;
                    if(f_len == k-1)
                    {
                        break;
                  }


              }
          }
          else if(M1.containsKey(Arr[counter]))
          {
              K2[M1.get(Arr[counter])]=counter;
          }
          counter++;
      }
      for(int j=f_end-f_len;j<=f_end;j++)
      {
          System.out.print(Arr_p[j]+" ");
      }
 }
 }
}

Tuesday, September 11, 2012

code chef September 2012

It's a nice experience. Started with the easy one HORSES, then move to another easy one CKISSHUG, CROWD having the unique pattern(required to generate the pattern in constant time), QNUMBER is about finding the total no. of divisor and CHEFTOWN where c++ stl fails giving tle and require to implement additional data structure to implement min() in (log n) time.

Its a nice experience having math stuffs, with fast I/O, choosing the right data structure .... and caring all the scenarios while writing the solution for all test cases.

Happy Learning ... :)

interviewstreet challenges (solved lucky number ...)

@total count is ten by solving lucky numbers with meeting point,flowers and pair. Lucky number the tougher one required DP with some calculation.... Keep coding :)