Letter Count

Write a function, ‘letter_count(str)’ that takes a string and
returns a hash mapping each letter to its frequency. Do not include
spaces:

Test Cases:

screen-shot-2016-11-18-at-5-42-12-pm

My Solution:

screen-shot-2016-11-21-at-12-37-37-pm

Runtime:  0.00059 seconds

Provided Solution:

Screen Shot 2016-11-21 at 12.38.20 PM.png

Runtime:  0.00053 seconds

By using each_char (an enumerator), you’re able to skip splitting the string into words and then into letters. Instead, you can directly pull letters from the string as is.

Advertisements
Letter Count

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s