📜  在给定的矩阵中按元音的出现顺序打印元音

📅  最后修改于: 2021-05-06 17:27:57             🧑  作者: Mango

鉴于一个字符矩阵ARR [] [尺寸3 * N,由三个字符{#,],。 },任务是从给定的字符串以’*’表示的元音( A,E,I,O,U )。

注意:元音A3×3块表示,如以下示例所示。

解释:

方法:想法是观察每个元音{‘A’,’E’,’I’,’O’,’E’}的点的行索引和列索引的模式,并检查每个j的以下条件列:

  1. 初始化最终结果, res为空字符串
  2. 如果arr [0] [j]等于‘#’ ,则将“#”附加到最终结果中。
  3. 如果arr [0] [j]等于‘。arr [1] [j]arr [2] [j]都等于‘。 ,表示空白。
  4. 如果arr [0] [j]等于‘。并且arr [0] [j + 2]等于‘。并且arr [2] [j + 1]等于‘。 ,然后在最终结果后附加“ A”
  5. 如果arr [0] [j + 1]等于‘。并且arr [1] [j + 1]等于‘。 ,然后在最终结果后附加“ U”
  6. 如果arr [0] [j + 1]不等于‘。并且arr [1] [j + 1]等于‘。 ,然后在最终结果后附加“ O”
  7. 如果arr [1] [j]等于‘。并且arr [1] [j + 2]等于‘。 ,然后将“ I”添加到最终结果中。
  8. 否则,将“ E”添加到最终结果中。

下面是上述方法的实现:

C++
* . * # * * * # * * * # * * * . * .
* . * # * . * # . * . # * * * * * *
* * * # * * * # * * * # * * * * . *


Java
* . * # . * * * # . * .
* . * # . . * . # * * *
* * * # . * * * # * . *


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


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

时间复杂度: O(N)
辅助空间:O(1)