Skip to content

Commit

Permalink
Added two string problems
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvasj committed Oct 28, 2024
1 parent 7a3b992 commit 92c22ad
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
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)

33 changes: 33 additions & 0 deletions Algorithms_and_Data_Structures/Strings/valid_anagram.py
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)

0 comments on commit 92c22ad

Please sign in to comment.