In one operation, you can choose any row or column and flip each value in that row or column (i.e., changing all 0‘s to 1‘s, and all 1‘s to 0‘s).
Return trueif it is possible to remove all1‘s fromgrid using any number of operations or false otherwise.
Example 1:
Input: grid = [[0,1,0],[1,0,1],[0,1,0]] Output: true Explanation: One possible way to remove all 1’s from grid is to: - Flip the middle row - Flip the middle column
Example 2:
Input: grid = [[1,1,0],[0,0,0],[0,0,0]] Output: false Explanation: It is impossible to remove all 1’s from grid.
Example 3:
Input: grid = [[0]] Output: true Explanation: There are no 1’s in grid.
Constraints:
m == grid.lengthn == grid[i].length 1 <= m, n <= 300grid[i][j] is either 0 or 1.
defremoveOnes(self, grid: List[List[int]]) -> bool: row_patten, row_pattern_invert = grid[0], [1 - n for n in grid[0]] for i inrange(1, len(grid)): if grid[i] != row_patten and grid[i] != row_pattern_invert: returnFalse returnTrue
You are given a 0-indexed string s that you must perform k replacement operations on. The replacement operations are given as three 0-indexed parallel arrays, indices, sources, and targets, all of length k.
To complete the i<sup>th</sup> replacement operation:
1. Check if the substringsources[i] occurs at index indices[i] in the original strings. 2. If it does not occur, do nothing. 3. Otherwise if it does occur, replace that substring with targets[i].
For example, if s = "<u>ab</u>cd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then the result of this replacement will be "<u>eee</u>cd".
All replacement operations must occur simultaneously, meaning the replacement operations should not affect the indexing of each other. The testcases will be generated such that the replacements will not overlap.
For example, a testcase with s = "abc", indices = [0, 1], and sources = ["ab","bc"] will not be generated because the "ab" and "bc" replacements overlap.
Return the resulting string after performing all replacement operations ons.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = “abcd”, indices = [0, 2], sources = [“a”, “cd”], targets = [“eee”, “ffff”] Output: “eeebffff” Explanation: “a” occurs at index 0 in s, so we replace it with “eee”. “cd” occurs at index 2 in s, so we replace it with “ffff”.
Example 2:
Input: s = “abcd”, indices = [0, 2], sources = [“ab”,”ec”], targets = [“eee”,”ffff”] Output: “eeecd” Explanation: “ab” occurs at index 0 in s, so we replace it with “eee”. “ec” does not occur at index 2 in s, so we do nothing.
Constraints:1 <= s.length <= 1000 k == indices.length == sources.length == targets.length1 <= k <= 100 0 <= indexes[i] < s.length1 <= sources[i].length, targets[i].length <= 50 s consists of only lowercase English letters.
sources[i] and targets[i] consist of only lowercase English letters.
题目大意:
整洁题。找到位置,然后验证,最后替换
解题思路:
N/A
解题步骤:
N/A
注意事项:
i是循环外的变量,所以poplate index_dict注意不能重名
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
deffindReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str: res = '' index_dict = {} for _i, _n inenumerate(indices): index_dict[_n] = _i # 0 -> 0, 2 -> 1 i = 0 while i < len(s): if i in index_dict and s[i:i + len(sources[index_dict[i]])] == sources[index_dict[i]]: res += targets[index_dict[i]] i += len(sources[index_dict[i]]) else: res += s[i] i += 1 return res
You are given an array of unique strings wordlist where wordlist[i] is 6 letters long, and one word in this list is chosen as secret.
You may call Master.guess(word) to guess a word. The guessed word should have type string and must be from the original list with 6 lowercase letters.
This function returns an integer type, representing the number of exact matches (value and position) of your guess to the secret word. Also, if your guess is not in the given wordlist, it will return -1 instead.
For each test case, you have exactly 10 guesses to guess the word. At the end of any number of calls, if you have made 10 or fewer calls to Master.guess and at least one of these guesses was secret, then you pass the test case.
Example 1:
Input: secret = “acckzz”, wordlist = [“acckzz”,”ccbazz”,”eiowzz”,”abcczz”], numguesses = 10 Output: You guessed the secret word correctly. Explanation: master.guess(“aaaaaa”) returns -1, because “aaaaaa” is not in wordlist. master.guess(“acckzz”) returns 6, because “acckzz” is secret and has all 6 matches. master.guess(“ccbazz”) returns 3, because “ccbazz” has 3 matches. master.guess(“eiowzz”) returns 2, because “eiowzz” has 2 matches. master.guess(“abcczz”) returns 4, because “abcczz” has 4 matches. We made 5 calls to master.guess and one of them was the secret, so we pass the test case.
Example 2:
Input: secret = “hamada”, wordlist = [“hamada”,”khaled”], numguesses = 10 Output: You guessed the secret word correctly.
Constraints:
1 <= wordlist.length <= 100wordlist[i].length == 6 wordlist[i] consist of lowercase English letters.
All the strings of wordlist are unique. secret exists in wordlist.
numguesses == 10
deffindSecretWord2(self, wordlist, master): for i inrange(10): guess = wordlist[0] res = master.guess(guess) wordlist = [w for w in wordlist ifself.match(w, guess) == res]
defmatch(self, w1, w2): returnsum(i == j for i, j inzip(w1, w2))
deffindSecretWord(self, wordlist, master): for _ inrange(10): char_to_count = [collections.Counter(w[i] for w in wordlist) for i inrange(6)] guess = max(wordlist, key=lambda w: sum(char_to_count[i][char] for i, char inenumerate(w))) res = master.guess(guess) wordlist = [w for w in wordlist ifself.match(w, guess) == res]
defmatch(self, w1, w2): returnsum(i == j for i, j inzip(w1, w2))
You are asked to design a file system that allows you to create new paths and associate them with different values.
The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, “/leetcode" and “/leetcode/problems" are valid paths while an empty string "" and "/" are not.
Implement the FileSystem class:
bool createPath(string path, int value) Creates a new path and associates a value to it if possible and returns true. Returns false if the path already exists or its parent path doesn’t exist.
int get(string path) Returns the value associated with path or returns -1 if the path doesn’t exist.
it = self.head for i inrange(1, len(segments)): segment = segments[i] if segment notin it.children: if i == len(segments) - 1: # match all the previous segments it.children[segment] = TrieNode(segment) else: returnFalse it = it.children[segment] if it.value != -1: # exists returnFalse it.value = value returnTrue
it = self.head for i inrange(1, len(segments)): segment = segments[i] if segment notin it.children: return -1 it = it.children[segment] return it.value