-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
Algorithms_and_Data_Structures/Strings/longest_palindromic_substring.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |