版权声明:本文为博主原创文章,采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。欢迎转载,但请注明作者姓名和文章出处。 https://blog.csdn.net/njit_77/article/details/79748790
Challenge
Using the C# language, have the function
VowelSquare(strArr) take the
strArr parameter being passed which will be a 2D matrix of some arbitrary size filled with letters from the alphabet, and determine if a 2x2 square composed entirely of vowels exists in the matrix. For example:
strArr is ["abcd", "eikr", "oufj"] then this matrix looks like the following:
a b c d
e i k r
o u f j
Within this matrix there is a 2x2 square of vowels starting in the second row and first column, namely, ei, ou. If a 2x2 square of vowels is found your program should return the top-left position (row-column) of the square, so for this example your program should return 1-0. If no 2x2 square of vowels exists, then return the string not found. If there are multiple squares of vowels, return the one that is at the most top-left position in the whole matrix. The input matrix will at least be of size 2x2.
a b c d
e i k r
o u f j
Within this matrix there is a 2x2 square of vowels starting in the second row and first column, namely, ei, ou. If a 2x2 square of vowels is found your program should return the top-left position (row-column) of the square, so for this example your program should return 1-0. If no 2x2 square of vowels exists, then return the string not found. If there are multiple squares of vowels, return the one that is at the most top-left position in the whole matrix. The input matrix will at least be of size 2x2.
Sample Test Cases
Input:"aqrst", "ukaei", "ffooo"
Output:"1-2"
Input:"gg", "ff"
Output:"not found"
Vowel Square算法
1、输入字母矩阵,判断是否有元音字母组成的矩阵;
2、没有返回"not found",有或者多个就返回位于矩阵最左上角字母序号;
public static string VowelSquare(string[] strArr)
{
int row = strArr.Length;
int col = strArr[0].Length;
for (int i = 0; i < row - 1; i++)
{
for (int j = 0; j < col - 1; j++)
{
if (IsCharVowelCharacter(strArr[i][j])
&& IsCharVowelCharacter(strArr[i][j + 1])
&& IsCharVowelCharacter(strArr[i + 1][j])
&& IsCharVowelCharacter(strArr[i + 1][j + 1]))
{
return string.Format("{0}-{1}", i, j);
}
}
}
return "not found";
}
private static bool IsCharVowelCharacter(char ch)
{
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}