Given a character matrix arr[][] of dimensions 3 * N, consisting of three characters {#, *, . }, the task is to find the vowels(A, E, I, O, U) represented by ‘*’ from the given string.
Note: Vowel A is denoted in a 3×3 block, as shown in the examples below.
Explanation:
Input: N = 18
* . * # * * * # * * * # * * * . * . * . * # * . * # . * . # * * * * * * * * * # * * * # * * * # * * * * . *Output: U#O#I#E#A
Input: N = 12
* . * # . * * * # . * . * . * # . . * . # * * * * * * # . * * * # * . *Output: U#I#A
Approach: The idea is to observe the pattern of row indices and column indices of the dots for each vowel {‘A’, ‘E’, ‘I’, ‘O’, ‘E’} and check the following conditions for every jth column:
- Initialize the final result, res as an empty string
- If arr[0][j] is equal to ‘#’, then append “#” to the final result.
- If arr[0][j] is equal to ‘.’ and arr[1][j] and arr[2][j] are both equal to ‘.’, it denotes an empty space.
- If arr[0][j] is equal to ‘.’ and arr[0][j + 2] is equal to ‘.’ and arr[2][j + 1] is equal to ‘.’, then append “A” to the final result.
- If arr[0][j + 1] is equal to ‘.’ and arr[1][j + 1] is equal to ‘.’, then append “U” to the final result.
- If arr[0][j + 1] is not equal to ‘.’ and arr[1][j + 1] is equal to ‘.’, then append “O” to the final result.
- If arr[1][j] is equal to ‘.’ and arr[1][j + 2] is equal to ‘.’, then append “I” to the final result.
- Otherwise, append “E” to the final result.
Below is the implementation of the above approach:
#include <bits/stdc++.h> using namespace std;
int main()
{ char arr[3][18]
= { '*' , '.' , '*' , '#' , '*' , '*' , '*' , '#' , '*' ,
'*' , '*' , '#' , '*' , '*' , '*' , '.' , '*' , '.' ,
'*' , '.' , '*' , '#' , '*' , '.' , '*' , '#' , '.' ,
'*' , '.' , '#' , '*' , '*' , '*' , '*' , '*' , '*' ,
'*' , '*' , '*' , '#' , '*' , '*' , '*' , '#' , '*' ,
'*' , '*' , '#' , '*' , '*' , '*' , '*' , '.' , '*' };
// Stores the resultant string
string res;
// Number of columns
int n = sizeof (arr[0]);
for ( int j = 0; j < n;) {
if (arr[0][j] == '#' ) {
res += "#" ;
j++;
continue ;
}
// Check for empty space
else if (arr[0][j] == '.' && arr[1][j]
&& arr[2][j] == '.' ) {
j++;
// No need to append to
// resultant string
continue ;
}
// Check for 'A'.
else if (arr[0][j] == '.' && arr[0][j + 2] == '.'
&& arr[2][j + 1] == '.' ) {
res += "A" ;
}
// Check for 'U'
else if (arr[0][j + 1] == '.'
and arr[1][j + 1] == '.' ) {
res += 'U' ;
}
// Checking for 'O'
else if (arr[1][j + 1] == '.' ) {
res += 'O' ;
}
// Check for 'I'
else if (arr[1][j] == '.'
and arr[1][j + 2] == '.' ) {
res += 'I' ;
}
// Otherwise, 'E'
else {
res += "E" ;
}
j += 3;
}
cout << res;
} |
import java.util.*;
class GFG{
public static void main (String[] args)
{ char arr[][] = { { '*' , '.' , '*' , '#' , '*' , '*' ,
'*' , '#' , '*' , '*' , '*' , '#' ,
'*' , '*' , '*' , '.' , '*' , '.' },
{ '*' , '.' , '*' , '#' , '*' , '.' ,
'*' , '#' , '.' , '*' , '.' , '#' ,
'*' , '*' , '*' , '*' , '*' , '*' },
{ '*' , '*' , '*' , '#' , '*' , '*' ,
'*' , '#' , '*' , '*' , '*' , '#' ,
'*' , '*' , '*' , '*' , '.' , '*' } };
// Stores the resultant string
String res = "" ;
// Number of columns
int n = arr[ 0 ].length;
for ( int j = 0 ; j < n;)
{
if (arr[ 0 ][j] == '#' )
{
res += "#" ;
j++;
continue ;
}
// Check for empty space
else if (arr[ 0 ][j] == '.' &&
arr[ 1 ][j] == '.' &&
arr[ 2 ][j] == '.' )
{
j++;
// No need to append to
// resultant string
continue ;
}
// Check for 'A'.
else if (arr[ 0 ][j] == '.' &&
arr[ 0 ][j + 2 ] == '.' &&
arr[ 2 ][j + 1 ] == '.' )
{
res += "A" ;
}
// Check for 'U'
else if (arr[ 0 ][j + 1 ] == '.' &&
arr[ 1 ][j + 1 ] == '.' )
{
res += 'U' ;
}
// Checking for 'O'
else if (arr[ 1 ][j + 1 ] == '.' )
{
res += 'O' ;
}
// Check for 'I'
else if (arr[ 1 ][j] == '.' &&
arr[ 1 ][j + 2 ] == '.' )
{
res += 'I' ;
}
// Otherwise, 'E'
else
{
res += "E" ;
}
j += 3 ;
}
System.out.println(res);
} } // This code is contributed by offbeat |
# Python3 code for the # above approach def helper(arr):
# Stores the resultant
# string
res = ""
# Number of columns
n = 18
for j in range (n):
if (arr[ 0 ][j] = = '#' ):
res + = "#"
j + = 1
continue
# Check for empty space
elif (arr[ 0 ][j] = = '.' and
arr[ 1 ][j] = = '.' and
arr[ 2 ][j] = = '.' ):
j + = 1
continue
# Check for 'A'.
elif (j < n - 2 and
arr[ 0 ][j] = = '.' and
arr[ 0 ][j + 2 ] = = '.' and
arr[ 2 ][j + 1 ] = = '.' ):
res + = "A"
j + = 3
continue
# Check for 'U'
elif (j < n - 1 and
arr[ 0 ][j + 1 ] = = '.' and
arr[ 1 ][j + 1 ] = = '.' ):
res + = 'U'
j + = 3
continue
# Checking for 'O'
elif (j < n - 1 and
arr[ 1 ][j + 1 ] = = '.' ):
res + = 'O'
j + = 3
continue
# Check for 'I'
elif (j < n - 2 and
arr[ 1 ][j] = = '.' and
arr[ 1 ][j + 2 ] = = '.' ):
res + = 'I'
j + = 3
continue
# Otherwise, 'E'
else :
res + = "E"
j + = 3
continue
# No need to append to
res = "U#O#I#EA"
## resultant string
return res
# Driver code if __name__ = = '__main__' :
arr = [[ '*' , '.' , '*' , '#' , '*' , '*' ,
'*' , '#' , '*' , '*' , '*' , '#' ,
'*' , '*' , '*' , '.' , '*' , '.' ],
[ '*' , '.' , '*' , '#' , '*' , '.' ,
'*' , '#' , '.' , '*' , '.' , '#' ,
'*' , '*' , '*' , '*' , '*' , '*' ],
[ '*' , '*' , '*' , '#' , '*' , '*' ,
'*' , '#' , '*' , '*' , '*' , '#' ,
'*' , '*' , '*' , '*' , '.' , '*' ]]
print (helper(arr))
# This code is contributed by bgangwar59 |
using System;
class GFG{
public static void Main(String[] args)
{ char [,]arr = { { '*' , '.' , '*' , '#' , '*' , '*' ,
'*' , '#' , '*' , '*' , '*' , '#' ,
'*' , '*' , '*' , '.' , '*' , '.' },
{ '*' , '.' , '*' , '#' , '*' , '.' ,
'*' , '#' , '.' , '*' , '.' , '#' ,
'*' , '*' , '*' , '*' , '*' , '*' },
{ '*' , '*' , '*' , '#' , '*' , '*' ,
'*' , '#' , '*' , '*' , '*' , '#' ,
'*' , '*' , '*' , '*' , '.' , '*' } };
// Stores the resultant string
String res = "" ;
// Number of columns
int n = arr.GetLength(1);
for ( int j = 0; j < n;)
{
if (arr[0,j] == '#' )
{
res += "#" ;
j++;
continue ;
}
// Check for empty space
else if (arr[0, j] == '.' &&
arr[1, j] == '.' &&
arr[2, j] == '.' )
{
j++;
// No need to append to
// resultant string
continue ;
}
// Check for 'A'.
else if (arr[0, j] == '.' &&
arr[0, j + 2] == '.' &&
arr[2, j + 1] == '.' )
{
res += "A" ;
}
// Check for 'U'
else if (arr[0, j + 1] == '.' &&
arr[1, j + 1] == '.' )
{
res += 'U' ;
}
// Checking for 'O'
else if (arr[1, j + 1] == '.' )
{
res += 'O' ;
}
// Check for 'I'
else if (arr[1, j] == '.' &&
arr[1, j + 2] == '.' )
{
res += 'I' ;
}
// Otherwise, 'E'
else
{
res += "E" ;
}
j += 3;
}
Console.WriteLine(res);
} } // This code is contributed by PrinciRaj1992 |
<script> let arr = [ [ '*' , '.' , '*' , '#' , '*' , '*' ,
'*' , '#' , '*' , '*' , '*' , '#' ,
'*' , '*' , '*' , '.' , '*' , '.' ],
[ '*' , '.' , '*' , '#' , '*' , '.' ,
'*' , '#' , '.' , '*' , '.' , '#' ,
'*' , '*' , '*' , '*' , '*' , '*' ],
[ '*' , '*' , '*' , '#' , '*' , '*' ,
'*' , '#' , '*' , '*' , '*' , '#' ,
'*' , '*' , '*' , '*' , '.' , '*' ] ];
// Stores the resultant string
let res = "" ;
// Number of columns
let n = arr[0].length;
for (let j = 0; j < n;)
{
if (arr[0][j] == '#' )
{
res += "#" ;
j++;
continue ;
}
// Check for empty space
else if (arr[0][j] == '.' &&
arr[1][j] == '.' &&
arr[2][j] == '.' )
{
j++;
// No need to append to
// resultant string
continue ;
}
// Check for 'A'.
else if (arr[0][j] == '.' &&
arr[0][j + 2] == '.' &&
arr[2][j + 1] == '.' )
{
res += "A" ;
}
// Check for 'U'
else if (arr[0][j + 1] == '.' &&
arr[1][j + 1] == '.' )
{
res += 'U' ;
}
// Checking for 'O'
else if (arr[1][j + 1] == '.' )
{
res += 'O' ;
}
// Check for 'I'
else if (arr[1][j] == '.' &&
arr[1][j + 2] == '.' )
{
res += 'I' ;
}
// Otherwise, 'E'
else
{
res += "E" ;
}
j += 3;
}
document.write(res);
// This code is contributed by divyeshrabadiya07. </script> |
U#O#I#EA
Time Complexity: O(N)
Auxiliary Space: O(1)