mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-13 08:01:03 +01:00
remove duplication
This commit is contained in:
parent
dc504fc1c0
commit
515b076e61
1 changed files with 1 additions and 36 deletions
|
@ -17,6 +17,7 @@
|
|||
#
|
||||
# Approach: Hash table
|
||||
#
|
||||
|
||||
# Complexity analysis:
|
||||
#
|
||||
# Time complexity: O(n). Time complexity is O(n) since accessing the counter
|
||||
|
@ -59,42 +60,6 @@ t = 'ab'
|
|||
puts(is_anagram(s, t))
|
||||
# => false
|
||||
|
||||
|
||||
def is_anagram(s, t)
|
||||
s_length = s.length
|
||||
t_length = t.length
|
||||
counter = Hash.new(0)
|
||||
|
||||
return false unless s_length == t_length
|
||||
|
||||
(0...s_length).each do |i|
|
||||
counter[s[i]] += 1
|
||||
end
|
||||
|
||||
(0...s_length).each do |i|
|
||||
counter[t[i]] -= 1
|
||||
|
||||
return false if counter[t[i]] < 0
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
s = 'anagram'
|
||||
t = 'nagaram'
|
||||
puts(is_anagram(s, t))
|
||||
# => true
|
||||
|
||||
s = 'rat'
|
||||
t = 'car'
|
||||
puts(is_anagram(s, t))
|
||||
# => false
|
||||
|
||||
s = 'a'
|
||||
t = 'ab'
|
||||
puts(is_anagram(s, t))
|
||||
# => false
|
||||
|
||||
#
|
||||
# Approach 2: Hash table
|
||||
#
|
||||
|
|
Loading…
Reference in a new issue