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 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 single integer N denoting number of columns. Output Output contains vowels (stars) in order of their occurrence within the given galaxy. Galaxy itself is represented by # character. Time Limit 1 Examples Example 1 Input 18 * . * # * * * # * * * # * * * . * . * . * # * . * # . * . # * * * * * * * * * # * * * # * * * # * * * * . * Output U#O#I#EA Explanation As it can be seen that the stars make the image of the alphabets U, O, I, E and A respectively.
Answers
Answer:
The code for the above problem in C++, python is
Explanation:
1. C++
#include <bits/stdc++.h>
using namespace std;
void printGalaxy(
vector<vector<char> > mat, int n)
{
for (int i = 0; i < n; i++) {
if (mat[0][i] == '#'
&& mat[1][i] == '#'
&& mat[2][i] == '#') {
cout << '#';
}
else if (mat[0][i] == '.'
&& mat[1][i] == '.'
&& mat[2][i] == '.') {
}
else {
char a, b, c, a1, b1;
char c1, a2, b2, c2;
int x1 = i;
a = mat[0][x1];
b = mat[0][x1 + 1];
c = mat[0][x1 + 2];
a1 = mat[1][x1];
b1 = mat[1][x1 + 1];
c1 = mat[1][x1 + 2];
a2 = mat[2][x1];
b2 = mat[2][x1 + 1];
c2 = mat[2][x1 + 2];
if (a == '.' && b == '*'
&& c == '.' && a1 == '*'
&& b1 == '*' && c1 == '*'
&& a2 == '*' && b2 == '.'
&& c2 == '*') {
cout << "A";
i = i + 2;
}
if (a == '*' && b == '*'
&& c == '*' && a1 == '*'
&& b1 == '*' && c1 == '*'
&& a2 == '*' && b2 == '*'
&& c2 == '*') {
cout << "E";
i = i + 2;
}
if (a == '*' && b == '*'
&& c == '*' && a1 == '.'
&& b1 == '*' && c1 == '.'
&& a2 == '*' && b2 == '*'
&& c2 == '*') {
cout << "I";
i = i + 2;
}
if (a == '*' && b == '*'
&& c == '*' && a1 == '*'
&& b1 == '.' && c1 == '*'
&& a2 == '*' && b2 == '*'
&& c2 == '*') {
cout << "O";
i = i + 2;
}
if (a == '*' && b == '.'
&& c == '*' && a1 == '*'
&& b1 == '.' && c1 == '*'
&& a2 == '*' && b2 == '*'
&& c2 == '*') {
cout << "U";
i = i + 2;
}
}
}
}
int main()
{
int N = 18;
vector<vector<char> > mat
= { { '*', '.', '*', '#', '*', '*', '*', '#', '*',
'*', '*', '#', '*', '*', '*', '.', '*', '.' },
{ '*', '.', '*', '#', '*', '.', '*', '#', '.',
'*', '.', '#', '*', '*', '*', '*', '*', '*' },
{ '*', '*', '*', '#', '*', '*', '*', '#', '*',
'*', '*', '#', '*', '*', '*', '*', '.',
'*' } };
printGalaxy(mat, N);
return 0;
}
2. Python
def printGalaxy(mat, n):
for i in range(n - 2):
if (mat[0][i] == '#' and mat[1][i] == '#' and mat[2][i] == '#'):
print('#',end="");
elif(mat[0][i] == '.' and mat[1][i] == '.' and mat[2][i] == '.'):
p = 0;
else:
x1 = i;
a = mat[0][x1];
b = mat[0][x1 + 1];
c = mat[0][x1 + 2];
a1 = mat[1][x1];
b1 = mat[1][x1 + 1];
c1 = mat[1][x1 + 2];
a2 = mat[2][x1];
b2 = mat[2][x1 + 1];
c2 = mat[2][x1 + 2];
if (a == '.' and b == '*' and c == '.' and a1 == '*' and b1 == '*' and c1 == '*' and a2 == '*' and b2 == '.'
and c2 == '*'):
print("A",end="");
i = i + 2;
if (a == '*' and b == '*' and c == '*' and a1 == '*' and b1 == '*' and c1 == '*' and a2 == '*' and b2 == '*'
and c2 == '*'):
print("E",end="");
i = i + 2;
if (a == '*' and b == '*' and c == '*' and a1 == '.' and b1 == '*' and c1 == '.' and a2 == '*' and b2 == '*'
and c2 == '*'):
print("I",end="");
i = i + 2;
if (a == '*' and b == '*' and c == '*' and a1 == '*' and b1 == '.' and c1 == '*' and a2 == '*' and b2 == '*'
and c2 == '*'):
print("O",end="");
i = i + 2;
if (a == '*' and b == '.' and c == '*' and a1 == '*' and b1 == '.' and c1 == '*' and a2 == '*' and b2 == '*'
and c2 == '*'):
print("U",end="");
i = i + 2;
if __name__ == '__main__':
N = 18;
mat = [[ '*', '.', '*', '#', '*', '*', '*', '#', '*', '*', '*', '#', '*', '*', '*', '.', '*', '.'] ,
[ '*', '.', '*', '#', '*', '.', '*', '#', '.', '*', '.', '#', '*', '*', '*', '*', '*', '*' ],
[ '*', '*', '*', '#', '*', '*', '*', '#', '*', '*', '*', '#', '*', '*', '*', '*', '.', '*' ] ];
printGalaxy(mat, N);
#SPJ2