Interview Query

N-gram Dictionary

Start Timer

0:00:00

Upvote
10
Downvote
Save question
Mark as completed
View comments (20)
Next question

Write a function get_ngrams to take in a word (string) and return a dictionary of n-grams and their frequency in the given string.

Example 1:

Input:

string = 'banana' 
n=2

Output:

output = {'ba':1, 'an':2, 'na':2} 

Example 2:

Input:

string = 'banana' 
n=3 

Output:

output = {'ban':1, 'ana':2, 'nan':1}
.
.
.
.
.


Comments

Loading comments...