mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-29 22:24:07 +01:00
Merge pull request #116 from jsca-kwok/jk-anagram
Anagram: sorting approach
This commit is contained in:
commit
3bc8a108e4
2 changed files with 46 additions and 0 deletions
|
@ -18,6 +18,7 @@
|
||||||
* [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb)
|
* [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb)
|
||||||
* [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
|
* [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
|
||||||
* Strings
|
* Strings
|
||||||
|
* [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb)
|
||||||
* [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb)
|
* [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb)
|
||||||
* [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb)
|
* [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb)
|
||||||
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb)
|
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb)
|
||||||
|
|
45
data_structures/arrays/strings/anagram_checker.rb
Normal file
45
data_structures/arrays/strings/anagram_checker.rb
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
# 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}
|
||||||
|
|
||||||
|
#
|
||||||
|
# Approach 1: Sort and Compare
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
def is_anagram(s, t)
|
||||||
|
return false if s.length != t.length
|
||||||
|
|
||||||
|
arr1 = s.split('').sort
|
||||||
|
arr2 = t.split('').sort
|
||||||
|
|
||||||
|
arr1 == arr2
|
||||||
|
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
|
Loading…
Reference in a new issue