defdfs(root): ifnot root: return0 nonlocal max_len left = dfs(root.left) + 1 right = dfs(root.right) + 1 res = max(left, right) total = left + right - 1 max_len = max(res, total, max_len) return res
defstrWithout3a3b(self, a: int, b: int) -> str: res = '' while a > 0or b > 0: if a > b: iflen(res) > 1and res[-2] == res[-1] == 'a': res += 'b' b -= 1 else: res += 'a' a -= 1 else: iflen(res) > 1and res[-2] == res[-1] == 'b': res += 'a' a -= 1 else: res += 'b' b -= 1 return res
Given an m x n grid of characters board and a string word, return trueifwordexists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example 1:
<pre>Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
</pre>
Example 2:
<pre>Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output: true
</pre>
Example 3:
<pre>Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false
</pre>
Constraints:
m == board.length
n = board[i].length
1 <= m, n <= 6
1 <= word.length <= 15
board and word consists of only lowercase and uppercase English letters.
Follow up: Could you use search pruning to make your solution faster with a larger board?
Given an m x nboard of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
<pre>Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
</pre>
Example 2:
<pre>Input: board = [["a","b"],["c","d"]], words = ["abcb"]
Output: []
</pre>
deffindWords(self, board: List[List[str]], words: List[str]) -> List[str]: ifnot board ornot board[0] ornot words: return [] trie, res = Trie(), [] for word in words: trie.insert(word)
visited = [[Falsefor _ inrange(len(board[0]))] for _ inrange(len(board))] for i inrange(len(board)): for j inrange(len(board[0])): self.dfs(board, i, j, visited, trie, trie.get_head(), '', res) returnlist(res)
defdfs(self, board, start_x, start_y, visited, trie, trie_node, path, res): if start_x < 0or start_x >= len(board) or start_y < 0or start_y >= len(board[0]): return if visited[start_x][start_y]: return trie_child = trie.search_one_node(board[start_x][start_y], trie_node) ifnot trie_child: return visited[start_x][start_y] = True path += board[start_x][start_y] if trie_child.is_end: res.append(path) trie_child.is_end = False iflen(trie_child.children) == 0and board[start_x][start_y] in trie_node.children: trie_node.children.pop(board[start_x][start_y]) for dx, dy in OFFSETS: self.dfs(board, start_x + dx, start_y + dy, visited, trie, trie_child, path, res) path = path[:-1] visited[start_x][start_y] = False iflen(trie_child.children) == 0and board[start_x][start_y] in trie_node.children: trie_node.children.pop(board[start_x][start_y])
A website domain "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com" and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.
A count-paired domain is a domain that has one of the two formats "rep d1.d2.d3" or "rep d1.d2" where rep is the number of visits to the domain and d1.d2.d3 is the domain itself.
For example, "9001 discuss.leetcode.com" is a count-paired domain that indicates that discuss.leetcode.com was visited 9001 times.
Given an array of count-paired domainscpdomains, return an array of the count-paired domains of each subdomain in the input. You may return the answer in any order.
Example 1:
<pre>Input: cpdomains = ["9001 discuss.leetcode.com"]
Output: ["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
Explanation: We only have one website domain: "discuss.leetcode.com".
As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.
</pre>
Example 2:
<pre>Input: cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
Explanation: We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times.
For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
</pre>
Constraints:
1 <= cpdomain.length <= 100
1 <= cpdomain[i].length <= 100
cpdomain[i] follows either the "rep<sub>i</sub> d1<sub>i</sub>.d2<sub>i</sub>.d3<sub>i</sub>" format or the "rep<sub>i</sub> d1<sub>i</sub>.d2<sub>i</sub>" format.
rep<sub>i</sub> is an integer in the range [1, 10<sup>4</sup>].
d1<sub>i</sub>, d2<sub>i</sub>, and d3<sub>i</sub> consist of lowercase English letters.