[Leetcode] 422. Valid Word Square 解题报告
2017-10-30 16:13
253 查看
题目:
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
Example 1:
Example 2:
Example 3:
思路:
一道相对简单的题目,唯一需要注意的是边界条件,以及判断字符串长度方面的细节。算法的时间复杂度是O(n^2)。
代码:
class Solution {
public:
bool validWordSquare(vector<string>& words) {
int row_num = words.size();
for(int i = 0; i < row_num; ++i) {
if(words[i].length() > row_num) {
return false;
}
for(int j = i + 1; j < row_num; ++j) {
if(j < words[i].length()) { // first part
if(words[j].size() < i + 1 || words[j][i] != words[i][j]) {
return false;
}
}
else { // j >= words[i].length()
if(words[j].size() >= i + 1) {
return false;
}
}
}
}
return true;
}
};
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.
思路:
一道相对简单的题目,唯一需要注意的是边界条件,以及判断字符串长度方面的细节。算法的时间复杂度是O(n^2)。
代码:
class Solution {
public:
bool validWordSquare(vector<string>& words) {
int row_num = words.size();
for(int i = 0; i < row_num; ++i) {
if(words[i].length() > row_num) {
return false;
}
for(int j = i + 1; j < row_num; ++j) {
if(j < words[i].length()) { // first part
if(words[j].size() < i + 1 || words[j][i] != words[i][j]) {
return false;
}
}
else { // j >= words[i].length()
if(words[j].size() >= i + 1) {
return false;
}
}
}
}
return true;
}
};
相关文章推荐
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告
- [Leetcode] 169. Majority Element 解题报告
- [Leetcode] 758. Bold Words in String 解题报告
- 【LeetCode】Container With Most Water 解题报告
- [Leetcode] 49. Group Anagrams 解题报告
- leetCode解题报告5道题(三)
- LeetCode 292.Nim Game 解题报告
- [leetcode]110. Balanced Binary Tree@Java解题报告
- 【LeetCode】199.Binary Tree Right Side View(Medium)解题报告
- [LeetCode] Sqrt(int x)解题报告之无限逼近(牛顿法)
- [leetcode] 230. Kth Smallest Element in a BST 解题报告
- 【LeetCode】513.Find Bottom Left Tree Value(Medium)解题报告
- LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver
- Leetcode 434. Number of Segments in a String 字符串的分段 解题报告
- [LeetCode] First Missing Positive 解题报告
- [leetcode] 231. Power of Two 解题报告
- [LeetCode] Path Sum II 解题报告
- leetcode解题报告:121 Best Time to Buy and Sell Stock
- [LeetCode] Subsets II 解题报告
- [Leetcode] 341. Flatten Nested List Iterator 解题报告