📌  相关文章
📜  按照它们在给定矩阵中出现的顺序打印元音

📅  最后修改于: 2021-09-06 06:10:13             🧑  作者: Mango

给定一个维度为3 * N的字符矩阵arr[][] ,由三个字符{ # , * , . },任务是从给定的字符串由’*’表示的元音( A, E, I, O, U )。

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

解释:

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

  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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live