Print the Vowels in the Order of their occurrence in the given Matrix

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:  

  1. Initialize the final result, res as an empty string
  2. If arr[0][j] is equal to ‘#’, then append “#” to the final result.
  3. If arr[0][j] is equal to ‘.’ and arr[1][j] and arr[2][j] are both equal to ‘.’, it denotes an empty space.
  4. 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.
  5. If arr[0][j + 1] is equal to ‘.’ and arr[1][j + 1] is equal to ‘.’, then append “U” to the final result.
  6. If arr[0][j + 1] is not equal to ‘.’ and arr[1][j + 1] is equal to ‘.’, then append “O” to the final result.
  7. If arr[1][j] is equal to ‘.’ and arr[1][j + 2] is equal to ‘.’, then append “I” to the final result.
  8. Otherwise, append “E” to the final result.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: 

U#O#I#EA





 

Time Complexity: O(N) 
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

I am a tech enthusias

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.