TheAlgorithms-Ruby/data_structures/arrays/strings/anagram_checker.rb

46 lines
1 KiB
Ruby
Raw Normal View History

2021-03-26 18:43:21 +01:00
# Challenge name: Is anagram
#
# Given two strings s and t , write a function to determine
# if t is an anagram of s.
#
# Note:
# You may assume the string contains only lowercase alphabets.
#
# Follow up:
# What if the inputs contain unicode characters?
# How would you adapt your solution to such case?
#
# @param {String} s
# @param {String} t
# @return {Boolean}
2021-03-30 23:19:43 +02:00
#
2021-04-01 04:02:57 +02:00
# Approach: Sort and Compare
2021-03-30 23:19:43 +02:00
#
# Complexity analysis:
#
# Time Complexity: O(n log n). Assume that n is the length of s, sorting costs O(n log n), and comparing two strings costs O(n). Sorting time dominates and the overall time complexity is O(n log n).
# Space Complexity: O(1). Space depends on the sorting implementation which, usually, costs O(1) auxiliary space if heapsort is used.
2021-03-30 23:19:43 +02:00
#
2021-03-26 18:43:21 +01:00
def is_anagram(s, t)
2021-03-30 23:19:43 +02:00
return false if s.length != t.length
arr1 = s.split('').sort
arr2 = t.split('').sort
arr1 == arr2
2021-03-26 18:43:21 +01:00
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