Code2pdf 6745a8e0eefda
Code2pdf 6745a8e0eefda
class Solution:
def secondMostFrequentElement(self, nums):
if len(nums) < 2:
return -1 # If the input has less than 2 elements, no second most frequent element
second_largest_value = unique_values[1]
# Step 4: Find the first key with the second largest frequency
for key, value in sorted_items:
if value == second_largest_value:
return key
#Sum of highest and lowest
class Solution:
def sumHighestAndLowestFrequency(self, nums):
hash_array=[0]*(max(nums)+1)
for i in nums:
hash_array[i]=hash_array[i]+1
hash_dict = {j: hash_array[j] for j in range(len(hash_array))if hash_array[j] > 0}
sorted_items = sorted(hash_dict.items(), key=lambda x: x[1], reverse=True)
z=(max(x[1] for x in sorted_items))
x=(min(x[1] for x in sorted_items))
return x+z
#Reverse a string using no extra space and 2 Pointer approch ( swap)
class Solution:
def reverseString(self, s):
l=0
r=len(s)-1
while l<r:
s[l],s[r]=s[r],s[l]
l=l+1
r=r-1
return s
# Find largest odd subarray in a stirng
class Solution:
def largeOddNum(self, s):
right = len(s) - 1
while right >= 0:
if int(s[right]) % 2 != 0:
break
right -= 1
if right < 0:
return "" # No odd digits found
return s[left:right+1]
# Find largest/longest common prefix string amongst an array of strings.(again use greeedy approcah to filter)
class Solution:
def longestCommonPrefix(self, st):
z = st[::-1] # Reverse the list
smallest_word = min(z, key=len) # Find the shortest string
checker = str(smallest_word) # Use the shortest string as the initial checker
for i in z:
if checker in i: # Check if checker is a substring of i
if checker == i and len(st) > 1: # Ensure there are multiple strings
continue # Continue checking for a common prefix
elif checker == i:
return "" # Return empty if only one string matches
else:
checker = checker[:-1] # Shorten checker if no match
#isomorphic strinf (unique mapping )
class Solution:
def isomorphicString(self, s, t):
mapping = {} # Dictionary to map characters from s to t
for char1, char2 in zip(s, t):
# Check if char1 already has a mapping
if char1 in mapping:
if mapping[char1] != char2: # Check if the mapping is consistent
return False
else:
# Ensure char2 is not already mapped to another char1
if char2 in mapping.values():
return False
mapping[char1] = char2 # Add the mapping
return True
#
# rotation of array to create subarray of same leght and find goal string
class Solution:
def rotateString(self, s, goal):
z=[]
n=len(s)
for i in range(n): # Rotate the string `n` times
s = s[1:] + s[0] # Move the first character to the end
z.append(s)
if goal in z:
return True
else:
return False
# valid anangram solution for making substring of same legth
class Solution:
def anagramStrings(self, s, t):
hash_array_s = {}
hash_array_t={}
for char in s:
if char in hash_array_s:
hash_array_s[char] += 1
else:
hash_array_s[char] = 1
for z in t:
if z in hash_array_t:
hash_array_t[z] += 1
else:
hash_array_t[z] = 1
z= dict(sorted(hash_array_t.items()))
if z==hash_array_s:
return True
else:
return False
# sort hashamap by frequency and alphabetical order ie give ert for tree
class Solution:
def frequencySort(self, s):
hash_array_s = {}
for char in s:
if char in hash_array_s:
hash_array_s[char] += 1
else:
hash_array_s[char] = 1
sorted_keys = ''.join([key for key, _ in sorted(hash_array_s.items(), key=lambda item: (-item[1], item[0]))])
return sorted_keys