Problem 8 Solution-Anagram Check Analysis Using Python - Competitive Programming

preview_player
Показать описание
Get unlimited access to all courses by top Educators with an Unacademy Plus Subscription here -
Use Code: KN06
Pareeksha Series (Full length Mock test)
Unlock code: KN06
All Playlist In My channel

Please donate if you want to support the channel through GPay UPID,

Please join as a member in my channel to get additional benefits like materials in Data Science, live streaming for Members and many more

Please do subscribe my other channel too

Connect with me here:
Рекомендации по теме
Комментарии
Автор

Revise The Competitive Programming Language playlist

krishnaik
Автор

Thank you for the different test cases and types of errors you caught will be really helpful to write more efficient code.

sonalighadage
Автор

Thankyou sir for such valuable knowledge

rankushvishwakarma
Автор

basically same idea as yours but a bit more readable solution along with probably the best example to check the solution/

from collections import Counter
def anagram_ques(s1, s2):
s1=s1.replace(" ", '').lower()
s2=s2.replace(" ", '').lower()
return Counter(s1)==Counter(s2)
s1='tom marvolo riddle'
s2='i am lord voldemort'
anagram_ques(s1, s2)

answer to next question (group anagrams):-
def groupAnagrams(strs) :
h=defaultdict(list)
for i in strs:
", "")))].append(''.join(i))
return h.values()

ravitanwar
Автор

a=input()
b=input()
def anagram(a, b):
for i in a:
if a.count(i)==b.count(i):
return print(f'{a} and {b} are anagram')
return print(f'{a} and {b} are not anagram')
anagram(a, b)

sumanthhabib
Автор

Excellent learning platform for coder's

Neon_-xogn
Автор

will the time complexity reduces if we use "group_letters.get(i)" instead of "i in group_letters" in line 4 ? (It return None if it is not present).

nishu
Автор

used sorted: def anagram(str1, str2):
return sorted(str1) == sorted(str2)

str1="elbow"
str2="below"

check = anagram(str1, str2)
print(check)

navidbinahmed
Автор

Thanks for your solution, but I think its time complexity will be 3n. Correct me if I'm wrong.

VivekKumar-uphb
Автор

second one works if slightly modified:

str1 = "eleven0 plus two"
str2 = "twelve1 plus one"

def anagram(str1, str2):
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()
if len(str1) == len(str2):
for i in str1:
if i in str2:
pass
else:
return False
return True
return False
anagram(str1, str2)

TopGHippie
Автор

Answer for the last part of question:
List of anagrams:

h=['cat ', 'dog', 'go d', 'tac', 'act', 'OGd']
sortedwords=[]
for i in h:
i=i.replace(" ", "").lower()

print(sortedwords)

d={}
for i in unique_words:
c=[]
for j in range(len(sortedwords)):
if i==sortedwords[j]:
c.append(h[j])
d[i]=c
print(d)

NikhilRaghav
Автор

This solution using ASCII conversion is not efficient too. Provides wrong output for an edge case like: "ab" and "Ab" strings.
On Leetcode, it passes 46 test cases out of 50. The below solution may be considered as an end to end edge-case solutions.

def isAnagram(self, str1: str, str2: str) -> bool:

str1 = str1.lower()
str2 = str2.lower()

if len(str1) != len(str2):
return False

char_count = {}

for char in str1:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1

for char in str2:
if char in char_count:
char_count[char] -= 1
if char_count[char] < 0:
return False
else:
return False

return True

navidbinahmed
Автор

My solution to today's Problem:
### Anagram Function:
def Anagram(str1, str2):
dict = {}
for i in range(len(str1)):
if ord(str1[i]) <= 122 and ord(str1[i])>=97:
if ord(str1[i])-32 not in dict:
dict[ord(str1[i])-32] = 1
else:
dict[ord(str1[i])-32] = dict[ord(str1[i])-32] + 1
else:
if ord(str1[i]) not in dict:
dict[ord(str1[i])] = 1
else:
dict[ord(str1[i])] = dict[ord(str1[i])] + 1

for i in range(len(str2)):
if ord(str2[i])<=122 and ord(str2[i])>=97:
if ord(str2[i])-32 in dict:
if dict[ord(str2[i])-32] != 0:
dict[ord(str2[i])-32] = dict[ord(str2[i])-32] - 1
else:
return 0
else:
return 0
else:
if ord(str2[i]) in dict:
if dict[ord(str2[i])] != 0:
dict[ord(str2[i])] = dict[ord(str2[i])] - 1
else:
return 0
else:
return 0

return 1


### Function to find all the anagrams:
def Anagrams_In_List(List):
for i in range(len(List)):
if List[i] != "":
print(List[i])
for j in range(i+1, len(List)):
if List[j] != "":
if Anagram(List[i], List[j]):
print(List[j])
List[j] = ""
print('\n')

List = ['Cat', 'TAC', 'tac', 'god', 'DOg']
Anagrams_In_List(List)



You told us not to use any functions so instead of converting every character to the lower case by using the ".lower()" function, I used the ASCII values of all the characters.
I think we should use the ASCII value method because in some other common languages like C++ we don't usually use any functions except the sort function(probably)
let me know your views on it. :)
Also, The time complexity of your solution will be O(n) not O(n^2)

akhilsoni
Автор

def anagram(string1, string2):
count = 0
if len(string1) == (string2):
for a in string1:
if a in string2:
pass
else:
return 'The entered strings are not anagram'
if string2[count] in string1:
pass
else:
return 'The entered strings are not anagram'
count += 1
return 'The entered strings are anagram'
else:
return 'The entered strings are not anagram'

string1 = input('Enter string one: ').replace(' ', '').lower()
string2 = input('Enter string two: ').replace(' ', '').lower()

anagram(string1, string2)

deepum
Автор

Abhi 2 minutes huye aur pura video dekhne ke pehle kisine Dislike kr diya!! LOL.. First watch and then try to give ur expression. video hi pura 19 minute ka hai 2 minute mai kaise dekh liya? bhai

hphp
Автор

Hey krish, I like your videos however I have been observing for the past couple of days that you have left making videos on deep learning. You started paper to code series I guess but then you didn't carry on that series. I can understand that it might not be easy however I would love to see you continuing that series. Lets leave these code optimization tricks for other kids on youtube. xD

smilebig
Автор

my answer is:

def anagram(str1, str2):
group_letters={}
for i in str1:
if i in group_letters:
group_letters[i]+=1
else:
group_letters[i]=1
for i in str2:
if i in group_letters:
group_letters[i]-=1
else:
group_letters[i]=1

for j in group_letters:
if group_letters[j]!=0:
return False
return True
Anagram_dictionay={}
list=["cat", "dog", "tac", "god", "act"]
count=1
for i in range(0, len(list)-1):

string1=list[i].replace(" ", "").lower()

if string1 in Anagram_dictionay:
continue
for k in range (i, len(list)-1):

string2=list[k+1]

string2=string2.replace(" ", "").lower()
var=anagram(string1, string2)
if(var):
list[k+1]=string1
print(string1)

if string1 in Anagram_dictionay:

else:
Anagram_dictionay[string1]=2

print(Anagram_dictionay)

Mhmd
Автор

str1=input("Enter First String:").replace(" ", "").lower()
str2=input("Enter Second String:").replace(" ", "").lower()
for i in str1:
str2 = str2.replace(i, "", 1)
if str2 == "":
print("Anangram")
else:
print("Not Anangram")

desinaveeravenkatasatyanar
Автор

words = ['car', 'bat', 'tac', 'tab', 'act', ]
anagrams = []
non_anagrams = []

for elements in range(0, len(words)):
found_anagram = False

for j in range (elements+1, len(words)):
if sorted(words[elements]) == sorted(words[j]):
print("The anagrams are: ", words[elements], "and", words[j])
found_anagram = True

if words[elements] not in anagrams:

if words[j] not in anagrams:
anagrams.append(words[j])

if not found_anagram and words[elements] not in anagrams:


print("The non-anagram words are: ", ', '.join(non_anagrams))

navidbinahmed
Автор

can someone help me with my homeworks Python for free?? 🥺

Mujahidabbastarar
welcome to shbcf.ru