Given two strings s and p, return an array of all the start indices ofp‘s anagrams ins. You may return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “cbaebabacd”, p = “abc” Output: [0,6] Explanation: The substring with start index = 0 is “cba”, which is an anagram of “abc”. The substring with start index = 6 is “bac”, which is an anagram of “abc”.
Example 2:
Input: s = “abab”, p = “ab” Output: [0,1,2] Explanation: The substring with start index = 0 is “ab”, which is an anagram of “ab”. The substring with start index = 1 is “ba”, which is an anagram of “ab”. The substring with start index = 2 is “ab”, which is an anagram of “ab”.
Constraints:
`1 <= s.length, p.length <= 3 104*sandp` consist of lowercase English letters.
defchangeNodes(self, existingTree, newTree) -> int: ifnot existingTree andnot newTree: return0 ifnot existingTree ornot newTree: return self.count(existingTree) + self.count(newTree) res = 0if existingTree.key == newTree.key and existingTree.val == newTree.val else1 existing_children_dict = self.get_children_dict(existingTree.children) new_tree_children_dict = self.get_children_dict(newTree.children) for key in existing_children_dict.keys() & new_tree_children_dict.keys(): # in both res += self.changeNodes(existing_children_dict[key], new_tree_children_dict[key]) for key in existing_children_dict.keys() - new_tree_children_dict.keys(): # in existing tree not in new tree res += self.count(existing_children_dict[key]) for key in new_tree_children_dict.keys() - existing_children_dict.keys(): # in new tree not in existing tree res += self.count(new_tree_children_dict[key]) return res
defcount(self, root): ifnot root: return0 res = 0 for child in root.children: res += self.count(child) return1 + res
defget_children_dict(self, children): key_to_node = {} for child in children: key_to_node[child.key] = child return key_to_node
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 in range(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 in range(1, len(segments)): segment = segments[i] if segment notin it.children: return-1 it = it.children[segment] return it.value
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y. Also two strings X and Y are similar if they are equal.
For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".
Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}. Notice that "tars" and "arts" are in the same group even though they are not similar. Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.
We are given a list strs of strings where every string in strs is an anagram of every other string in strs. How many groups are there?
1 <= strs.length <= 3001 <= strs[i].length <= 300 strs[i] consists of lowercase letters only.
All words in strs have the same length and are anagrams of each other.
题目大意:
单词列表中,可以分成多少组,每组里面的单词互相之间至少有一对可以通过交换一个位置变成另一个单词
解题思路:
典型求连通集个数,类似于Num of island,用BFS。难点在于怎么找到neighbor,用遍历每一个单词的方式
defnumSimilarGroups(self, strs: List[str]) -> int: ifnot strs: return0 visited, groups, word_set = set(), 0, set(strs) for s in strs: if s in visited: continue self.bfs(word_set, s, visited) groups += 1 return groups
defbfs(self, word_set, start, visited): queue = collections.deque([start]) visited.add(start) while queue: node = queue.popleft() for s in word_set: if s in visited: continue if node == s ornot self.buddyStrings(node, s): continue queue.append(s) visited.add(s)
defbuddyStrings(self, s: str, goal: str) -> bool: if len(s) != len(goal): returnFalse if s == goal and len(set(s)) < len(goal): # any dups returnTrue diff = [(a, b) for a, b in zip(s, goal) if a != b] returnTrueif len(diff) == 2and diff[0] == diff[1][::-1] elseFalse