Most Frequent N-gram

Company: flipkart_20aug

Difficulty: medium

Problem Statement

Most Frequent N-gram Problem Description An N-gram of a string is any substring of length N made of consecutive characters of that string. Given a string S and an integer N , find the most frequent N-gram of S , that is, the substring of length N that occurs the largest number of times in S , and report how many times it occurs. Occurrences may overlap: in aaaa , the 2-gram aa occurs 3 times (starting at positions 0, 1 and 2). If more than one N-gram has the highest frequency, report the one that appears first in S , that is, the one whose first occurrence starts at the smallest position. Function Description Complete the function get_ngram with the following parameters: string s : the string S int k : the N-gram length N It returns a string : the most frequent N-gram, a single space, and its number of occurrences (for example ana 2 ). (inferred -- the source editor shows the call get_ngram(s, k) but its return type is not visible; returning the whole output line keeps the function ide