You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
2.Find the length of the longest substring without repeating characters along with the substring
deflongest_substring_without_repeating_characters(s1):
n=len(s1)
l=0charset=set()
result=0resstring=""forrinrange(n):
whiles1[r] incharset:
charset.remove(s1[l])
l+=1charset.add(s1[r])
if (r-l+1) >result:
result=r-l+1resstring=s1[l:r+1]
returnresult, resstringresult, resultsubstring=longest_substring_without_repeating_characters("abababacabahgyvaba")
print(f"The length of the longest substring is {result} in {resultsubstring}")
3. Find the Longest substring with k distinct character
4. Find the longest substring with k distinct character along with the substring
deflongest_substring_with_k_distinct_character(s1, k):
n=len(s1)
max_freq= {}
res=""max_len=0l=0forrinrange(n):
max_freq[s1[r]] =max_freq.get(s1[r],0)+1whilelen(max_freq) >k:
max_freq[s1[l]] -=1ifmax_freq[s1[l]] ==0:
delmax_freq[s1[l]]
l+=1if (r-l+1) >max_len:
max_len=r-l+1res=s1[l:r+1]
returnmax_len, result_substringmax_length, result_substring=longest_substring_with_k_distinct_char("abcabcaabbccdeft", 3)
print(f"The length of the longest of the substring is {max_length} and the substring is {result_substring}")