Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-008

Character N-grams

Problem

Return all character n-grams of text as a list of strings, in order.

If n <= 0 or n > len(text), return an empty list.

Hint

Slide a window of width n across the string from left to right.

Test cases

case 1
in: ["hello",2]
expected: ["he","el","ll","lo"]
case 2
in: ["abc",3]
expected: ["abc"]
case 3
in: ["abc",4]
expected: []
case 4
in: ["a",1]
expected: ["a"]
case 5
in: ["cat",0]
expected: []
Starter codePython
def char_ngrams(text, n):
    # Your code here
    pass

5 test cases · run your code against them in the editor