ValidWordSquare2 [source code]


public class ValidWordSquare2 {
static
/******************************************************************************/
public class Solution {
    public boolean validWordSquare(List<String> words) {
        int m = words.size();
        if (m == 0) return false;
        String[] ar = words.toArray(new String[0]);
        char[][] letters = new char[m][];
        for (int i = 0; i < m; i++) {
            letters[i] = ar[i].toCharArray();
        }
        for (int i = 0; i < m; i++) {
            int n = letters[i].length;
            for (int j = 0; j < Math.max(m, n); j++) {
                char letterHorizontal = (i >= m || j >= n) ? 'N' : letters[i][j];
                char letterVertical = (j >= m || i >= letters[j].length) ? 'N' : letters[j][i];
                if (letterHorizontal != letterVertical) return false;
            }
        }
        return true;
    }
}
/******************************************************************************/

    public static void main(String[] args) {
        ValidWordSquare2.Solution tester = new ValidWordSquare2.Solution();
        List<List<String>> inputs = new ArrayList<>();
        inputs.add(Arrays.asList(new String[]{
            "abcd",
            "bnrt",
            "crmy",
            "dtye"
        }));
        inputs.add(Arrays.asList(new String[] {
            "abcd",
            "bnrt",
            "crm",
            "dt"
        }));
        inputs.add(Arrays.asList(new String[] {
            "ball",
            "area",
            "read",
            "lady"            
        }));
        inputs.add(Arrays.asList(new String[]{"ball","asee","lett","le"}));
        inputs.add(Arrays.asList(new String[]{"abc","b"}));
        int counter = 0;
        for (List<String> ls : inputs) System.out.println((++counter) + " -> " + tester.validWordSquare(ls));
    }
}

这里尝试用另外一个思路来检查 word 长度不齐的情况;

最后的速度是12(97.7), 病咩有加快, 这个有点奇怪;


Problem Description

Given a sequence of words, check whether it forms a valid word square.

A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).

Note:
The number of words given is at least 1 and does not exceed 500.
Word length will be at least 1 and does not exceed 500.
Each word contains only lowercase English alphabet a-z.
Example 1:

Input:
[
"abcd",
"bnrt",
"crmy",
"dtye"
]

Output:
true

Explanation:
The first row and first column both read "abcd".
The second row and second column both read "bnrt".
The third row and third column both read "crmy".
The fourth row and fourth column both read "dtye".

Therefore, it is a valid word square.
Example 2:

Input:
[
"abcd",
"bnrt",
"crm",
"dt"
]

Output:
true

Explanation:
The first row and first column both read "abcd".
The second row and second column both read "bnrt".
The third row and third column both read "crm".
The fourth row and fourth column both read "dt".

Therefore, it is a valid word square.
Example 3:

Input:
[
"ball",
"area",
"read",
"lady"
]

Output:
false

Explanation:
The third row reads "read" while the third column reads "lead".

Therefore, it is NOT a valid word square.
Seen this question in a real interview before? Yes No
Difficulty:Easy
Total Accepted:10K
Total Submissions:27.6K
Contributor: LeetCode
Companies
google
Similar Questions
Word Squares

results matching ""

    No results matching ""