We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 53dd99a commit a5ce710Copy full SHA for a5ce710
ruby/242-Valid-Anagrams.rb
@@ -0,0 +1,15 @@
1
+def is_anagram(s, t)
2
+ return false unless s.length == t.length
3
+
4
+ hash = Hash.new(0)
5
+ s.each_char.with_index do |_, idx|
6
+ hash[s[idx]] += 1
7
+ hash[t[idx]] -= 1
8
+ end
9
+ hash.all? { |_k, v| v.zero? }
10
+end
11
12
+# For nlogn time and O(1) space, can sort then check if equal
13
14
+ s.chars.sort.join == t.chars.sort.join
15
0 commit comments