Saturday, October 11, 2025
HomeData Modelling & AIPrint the Vowels in the Order of their occurrence in the given...

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++




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


Java




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




# 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


C#




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


Javascript




<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>


Output: 

U#O#I#EA

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32351 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11882 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7102 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS