5 examples of 'find anagrams of a string python' in Python

Every line of 'find anagrams of a string python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
12def findAnagrams(self, s, p):
13 """
14 :type s: str
15 :type p: str
16 :rtype: List[int]
17 """
18 if not s or not p:
19 return []
20 p_map = dict(collections.Counter(p))
21 results = []
22 for i in range(len(s)):
23 if s[i] in p_map:
24 checker = dict(collections.Counter(s[i:i+len(p)]))
25 if checker == p_map:
26 results.append(i)
27 return results
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

13def groupAnagrams(self, strs):
14 i = 0
15 hashmap = {}
16 while i < len(strs):
17 s = tuple(sorted(strs[i]))
18 if s in hashmap:
19 hashmap[s].append(strs[i])
20 else:
21 hashmap[s] = [strs[i]]
22 i += 1
23 return hashmap.values()
12def check_anagram(first_string, second_string):
13 str.lower(first_string)
14 str.lower(second_string)
15 first_string.replace(" ", "")
16 second_string.replace(" ", "")
17 first_string = ''.join(sorted(first_string))
18 second_string = ''.join(sorted(second_string))
19 if first_string[:] == second_string[:]:
20 print("This is an anagram.")
21 else:
22 print("This is not an anagram.")
65def checkAnagram(word1, word2):
66 '''
67 check if two words are anagram of one another
68 '''
69 w1 = ''.join(sorted(word1)).upper()
70 w2 = ''.join(sorted(word2)).upper()
71 return w1 == w2
33def get_group_anagrams(arr: List[str]) -> List[str]:
34 '''
35 字符串
36 Args:
37 strs: 字符串数组
38 Returns:
39 链表
40 '''
41 dic = {}
42 for ele in arr:
43 chs_arr = sorted(list(ele))
44 print(chs_arr)
45 strs = ''.join(chs_arr)
46 if not dic.keys().__contains__(strs):
47 dic[strs] = []
48 dic[strs].append(ele)
49 return dic.values()

Related snippets