Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-297

Grammar Valid Continuation Count

Problem

Count the distinct next characters that a grammar allows. words is the list of allowed complete strings and prefix is the text generated so far.

Return the number of distinct characters that appear immediately after prefix in at least one allowed word. Return 0 when no allowed word extends prefix.

Hint

Collect the character at position len(prefix) for every extending word into a set.

Test cases

case 1
in: [["cat","car","dog"],"ca"]
expected: 2
case 2
in: [["cat","car","cow"],"c"]
expected: 2
case 3
in: [["cat"],"cats"]
expected: 0
case 4
in: [[],"ab"]
expected: 0
case 5
in: [["hello","help"],"hel"]
expected: 2
Starter codePython
def grammar_valid_continuation_count(words, prefix):
    # Your code here
    pass

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