IQ 55: Determine if 2 words are anagrams of each other

preview_player
Показать описание
Write a program to determine if 2 words are anagrams of each other
Рекомендации по теме
Комментарии
Автор

just use sets
demo in python, in c# use HashSet<char> and for compare use SetEquals()

def check(first_word, second_word):
return set(first_word) == set(second_word)

print(check("elbow", "below"))
# true
print(check("elbow", "velow"))
# false
print(check("elbow", "belows")) #false

cruzbasco
Автор

My way to do it:

public boolean areAnagrams(String s1, String s2) {
if (s1.length() != s2.length()) return false;
char[] chars1 = s1.toCharArray();
char[] chars2 = s2.toCharArray();

Arrays.sort(chars1);
Arrays.sort(chars2);

return
}

ГеоргийТаптыгин-йн
Автор

We can solve it using O(n) time complexity and constant space

kowlutlaks
Автор

Is it possible to get the string value using scanner sir

helenangeline
Автор

In C#/LINQ oneliner:
if (s1.Length == s2.Length && s1.Length > 0 && s1.OrderBy(c => c).Where((c1, index) => c1 == s2.OrderBy(c => c).ElementAt(index)).Count() == s1.Length)

else Console.WriteLine("NOT anagram!");

jan