From 92c22adb9670f08b62ccef4bfe8bbfa290bddb8d Mon Sep 17 00:00:00 2001 From: apoorvasj Date: Mon, 28 Oct 2024 18:34:09 +0530 Subject: [PATCH] Added two string problems --- .../Strings/longest_palindromic_substring.py | 61 +++++++++++++++++++ .../Strings/valid_anagram.py | 33 ++++++++++ 2 files changed, 94 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Strings/longest_palindromic_substring.py create mode 100644 Algorithms_and_Data_Structures/Strings/valid_anagram.py diff --git a/Algorithms_and_Data_Structures/Strings/longest_palindromic_substring.py b/Algorithms_and_Data_Structures/Strings/longest_palindromic_substring.py new file mode 100644 index 0000000000..1e6ae0f366 --- /dev/null +++ b/Algorithms_and_Data_Structures/Strings/longest_palindromic_substring.py @@ -0,0 +1,61 @@ +''' +longest palindromic substring without DP +We will be using a two pointer approach (l and r are the two pointers). We will be moving +from the middle of the string to its extreme ends and check whether the characters +on either side of the string match. +''' +class longestPalindrome: + def longestPalindromeFinder(self, s: str) -> str: + #declare a variable to store the temporary length each time a palindrome is found + temp_len=0 + + #declare a maximum length variable to store the overall maximum length + max_len=0 + l=0 + r=0 + + for i in range(len(s)): + #odd palindrome + #start from the middle of the string + + l=i + r=i + + #As long as the character left pointer points to and right pointer points to, match + #increment the right pointer and decrement the left one. + while(l>=0 and r<=len(s)-1 and s[l]==s[r]): + temp_len= r-l+1 + + #if a new maximum length is found, store it in the max_len variable. + if(temp_len>max_len): + max_len=temp_len + start=l + + l-=1 + r+=1 + + #even palindrome + #start from the two middle-most characters + l=i + r=i+1 + + #the remaining procedure remains the same as that of odd palindrome. + while(l>=0 and r<=len(s)-1 and s[l]==s[r]): + temp_len= r-l+1 + if(temp_len>max_len): + max_len=temp_len + start=l + + l-=1 + r+=1 + #return a substring of the original string which contains the longest palindrome found so far. + return s[start:start+max_len] + +#example usage +if __name__=='__main__': + + #create an instance of the class longestPalindrome and print the result + longest=longestPalindrome() + result=longest.longestPalindromeFinder("abbabab") + print(result) + diff --git a/Algorithms_and_Data_Structures/Strings/valid_anagram.py b/Algorithms_and_Data_Structures/Strings/valid_anagram.py new file mode 100644 index 0000000000..1ffb1988f9 --- /dev/null +++ b/Algorithms_and_Data_Structures/Strings/valid_anagram.py @@ -0,0 +1,33 @@ +''' +we are going to use a hashmap approach to count the number of characters in both the strings and +finally compare both hashmaps. +''' +from collections import defaultdict + +class valid_anagram: + #method to determine if two strings are valid anagrams. + def isAnagram(self, s: str, t: str) -> bool: + + #initialize two dictionaries to be used as hashmaps. + dictis=defaultdict() + dictit=defaultdict() + + #count the occurences of each character in both the strings. + for i in s: + dictis[i]=dictis.get(i,0)+1 + + for i in t: + dictit[i]=dictit.get(i,0)+1 + + #if both the dictionaries are the same, return True + return True if dictis==dictit else False + +#example usage +if __name__=='__main__': + + #create an instance of the valid_anagram class. + anagram_checker=valid_anagram() + + #call the isAnagram method + result = anagram_checker.isAnagram("dusty","study") + print(result) \ No newline at end of file