Skip to content

Commit a5ce710

Browse files
committed
242
1 parent 53dd99a commit a5ce710

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

ruby/242-Valid-Anagrams.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
def is_anagram(s, t)
14+
s.chars.sort.join == t.chars.sort.join
15+
end

0 commit comments

Comments
 (0)