TCS Codevita : Constellation Code Solution

 Constellation

Three characters { #, *, . } represents a constellation of stars and galaxies in space. Each galaxy is demarcated by # characters. There can be one or many stars in a given galaxy. Stars can only be in the shape of vowels { A, E, I, O, U }. A collection of * in the shape of the vowels is a star. A star is contained in a 3x3 block. Stars cannot be overlapping. The dot(.) character denotes empty space.

Given 3xN matrix comprising of { #, *, . } character, find the galaxy and stars within them.

Note: Please pay attention to how vowel A is denoted in a 3x3 block in the examples section below.

 

Constraints

3 <= N <= 10^5

 

Input

Input consists of a single integer N denoting the number of columns.

 

Output

The output contains vowels (stars) in order of their occurrence within the given galaxy. The galaxy itself is represented by the # character.

 

Sample Input

18

* . * # * * * # * * * # * * * . * .

* . * # * . * # . * . # * * * * * *

* * * # * * * # * * * # * * * * . *

 

Sample Output

U#O#I#EA

 


Code

#include <iostream>

using namespace std;


int main()

{

  int n;                              // number of columns

  cin>>n;

 

  char arr[3][n];                     // taking array of 3 by n

 

  for(int i=0;i<3;i++)                // taking input

{

    for(int j=0;j<n;j++)

{

      cin>>arr[i][j];

    }

  }

 

  for(int i=0;i<n;i++)                            // accessing each column 1 by 1

  {

    if(arr[0][i]=='#' && arr[1][i]=='#' && arr[2][i]=='#')          // first check for '#' condition in same column

{

      cout<<'#';

    }

else if(arr[0][i]=='.' && arr[1][i]=='.' && arr[2][i]=='.')     // then check for '.' condition in same column

{

    

}

else                                    // check for vowels

{

      char a,b,c,a1,b1,c1,a2,b2,c2;           // variables for matrix elements

     

      a = arr[0][i];              // 0th row ith column

      b = arr[0][i+1];            // i+1th column

      c = arr[0][i+2];            // i+2th column

     

      a1 = arr[1][i];             // 1st row ith column

      b1 = arr[1][i+1];           // i+1th column

      c1 = arr[1][i+2];           // i+2th column

     

      a2 = arr[2][i];             // 2nd row ith column

      b2 = arr[2][i+1];           // i+1th column

      c2 = arr[2][i+2];           // i+2th column

     

      // now check for each vowel by comparing matrix elements

     

      // for A

      if(a=='.' && b=='*' && c=='.' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='.' && c2=='*')

 

      cout<<"A";

      }

      // for E

      else if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='*' && c2=='*')

 

        cout<<"E";

      }

      // for I

      else if(a=='*' && b=='*' && c=='*' && a1=='.' && b1=='*' && c1=='.' && a2=='*' && b2=='*' && c2=='*')

 

        cout<<"I";

      }

      // for O 

      else if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')

 

        cout<<"O";

      }

      // for U (default)

      else

 

        cout<<"U";

      }

     

      i=i+2;      // to save execution time skip to next matrix / column    

    }

  }

}



Input

12

* . * # . * * * # . * .

* . * # . . * . # * * *

* * * # . * * * # * . *

 

Output

U#I#A

 


Alternate Method 


The Dot Method

In this method we will only check the dots for all the vowels A, E, I, O, U.

For A
dots will be at a, c & b2

For I
dots will be at a1, c1

For O
dots will be at b1

For U
dots will be at b & b1

For E
There will be no dots hence we will keep E at default condition


Code 

#include <iostream>
using namespace std;

int main()
{
  int n;                              // number of columns
  cin>>n;
 
  char arr[3][n];                     // taking array of 3 by n
 
  for(int i=0;i<3;i++)                // taking input
{
    for(int j=0;j<n;j++)
{
      cin>>arr[i][j];
    }
  }
 
  for(int i=0;i<n;i++)                            // accessing each column 1 by 1
  {
    if(arr[0][i]=='#' && arr[1][i]=='#' && arr[2][i]=='#')              // first check for '#' condition in same column
{
      cout<<'#';
    }
else if(arr[0][i]=='.' && arr[1][i]=='.' && arr[2][i]=='.')         // then check for '.' condition in same column
{
    
}
else                                    // check for vowels
{
      char a,b,c,a1,b1,c1,a2,b2,c2;           // variables for matrix elements
     
      a = arr[0][i];                  // 0th row ith column
      b = arr[0][i+1];            // i+1th column
      c = arr[0][i+2];           // i+2th column
     
      a1 = arr[1][i];                 // 1st row ith column
      b1 = arr[1][i+1];           // i+1th column
      c1 = arr[1][i+2];          // i+2th column
     
      a2 = arr[2][i];                 // 2nd row ith column
      b2 = arr[2][i+1];           // i+1th column
      c2 = arr[2][i+2];          // i+2th column
     
     
      // now check for each vowel by comparing matrix elements
     
      // for A
      if(a=='.' && c=='.' && b2=='.')
 
      cout<<"A";
      }
      // for U
      else if(b=='.' && b1=='.')
 
        cout<<"U";
      }
      // for I
      else if(a1=='.' && c1=='.')
 
        cout<<"I";
      }
      // for O 
      else if(b1=='.')
 
        cout<<"O";
      }
      // for E (default)
      else
 
        cout<<"E";
      }
     
      i=i+2;      // to save execution time skip to next matrix / column    
    }
  }
}


Input

18

* . * # * * * # * * * # * * * . * .

* . * # * . * # . * . # * * * * * *

* * * # * * * # * * * # * * * * . *

 

Output

U#O#I#EA


!! Feel free to comment your solution in your favorite language :) !!

Post a Comment

0 Comments