Created
April 4, 2024 03:56
Revisions
-
codertcet111 created this gist
Apr 4, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ Leetcode 49 Group Anagram # @param {String[]} strs # @return {String[][]} def group_anagrams(strs) sum_ascii_hash = {} strs.each_with_index do |str, i| sorted_aa = str.chars.sort.join sum_ascii_hash["#{sorted_aa}"] ? sum_ascii_hash["#{sorted_aa}"] << i : sum_ascii_hash["#{sorted_aa}"] = [i] end sum_ascii_hash.values.map{|ind_arr| ind_arr.map{|ind| strs[ind]}} end # Partially correct approach by calculating ascii value # def old_group_anagrams(strs) # sum_ascii_hash = {} # strs.each_with_index do |str, i| # sum_key = str.split("").uniq.sum { |char| char.ord } # Ignore: #sum_key = str.split("").sum { |char| char.ord } # Ignore: #sum_key = str.split("").each_with_index.map{|char, i| char.ord * i}.sum # sum_ascii_hash["#{sum_key}"] ? sum_ascii_hash["#{sum_key}"] << i : sum_ascii_hash["#{sum_key}"] = [i] # end # sum_ascii_hash.values.map{|ind_arr| ind_arr.map{|ind| strs[ind]}} # end