diff --git a/arrays_and_strings/README.md b/arrays_and_strings/README.md index 6711b4d..24bd97c 100644 --- a/arrays_and_strings/README.md +++ b/arrays_and_strings/README.md @@ -137,9 +137,64 @@ def remove_duplicates(nums: list[int]) -> int:
+--- + +### anagrams + +
+ +* to determine if two strings are anagrams, there are a few approaches: + - sorting both strings should produce the same string (`O(N log(N))` time and `O(log(N))` space. + - if we map each character to a prime number and we mutliply each mapped number together, anagrams should have the same multiple (prime factor decomposition, `O(N)`). + - frequency counting of characters can determine whether they are anagram (`O(N)`). + +
+ +```python +def is_anagram(string1, string2) -> bool: + + string1 = string1.lower() + string2 = string2.lower() + + if len(string1) != len(string2): + return False + + for c in string1: + if c not in string2: + return False + + return True +``` + +
+ +--- + ---- -### check if permutation is palindrome +### palindromes + +
+ +* ways to determine if a string is a palindrome: + * reverse the string and they should be equal. + * have two pointers at the start and end of the string, moving the pointers until they meet. + +
+ +```python +def is_palindrome(sentence): + + sentence = sentence.strip(' ') + if len(sentence) < 2: + return True + + if sentence[0] == sentence[-1]: + return is_palindrome(sentence[1:-1]) + + return False +``` +
@@ -165,6 +220,8 @@ def is_permutation_of_palindromes(some_string):
+ + --- ### intersection of two arrays @@ -188,29 +245,6 @@ def intersect(nums1: list[int], nums2: list[int]) -> list[int]: --- -### check if anagram - -
- -```python -def is_anagram(string1, string2) -> bool: - - string1 = string1.lower() - string2 = string2.lower() - - if len(string1) != len(string2): - return False - - for c in string1: - if c not in string2: - return False - - return True -``` - -
- ---- ### check if isomorphic @@ -261,26 +295,7 @@ def diagonal_difference(arr): return diag_1, diag_2, abs(diag_1 - diag_2) ``` -
---- - -### is palindrome - -
- -```python -def is_palindrome(sentence): - - sentence = sentence.strip(' ') - if len(sentence) < 2: - return True - - if sentence[0] == sentence[-1]: - return is_palindrome(sentence[1:-1]) - - return False -```