Match a Variadic Function Signature
Learn this problemProblem statement
You are building a function library. Each registered function has a unique name, an ordered list of parameter-type tokens, and an isVariadic flag.
The four parallel inputs describe the library and one lookup:
functionNames[i]is the name of functioni.functionArgumentTypes[i]is its declared parameter-type list.isVariadic[i]says whether its final declared parameter is variadic.callArgumentTypesis the ordered argument-type list to match.
Return the names of all matching functions, in their original functionNames order.
Matching rules
- Type tokens match by exact, case-sensitive string equality.
- A non-variadic function matches only when its declared list has exactly the same length as the call and every token matches at the same position.
- For a variadic function, the final declared type must occur one or more times. The call must therefore contain at least as many arguments as the declared list.
- For a variadic declaration of length
m, positions0throughm - 2must match the fixed prefix. Every call position fromm - 1onward must equal the final declared type.
For example, the variadic declaration [String, Integer] matches [String, Integer] and [String, Integer, Integer], but it does not match [String].
Function
findMatchingFunctions(functionNames: String[], functionArgumentTypes: String[][], isVariadic: boolean[], callArgumentTypes: String[]) → String[]Examples
Example 1
functionNames = ["FuncA","FuncB","FuncC","FuncD","FuncE","FuncF","FuncG"]functionArgumentTypes = [["String","Integer","Integer"],["String","Integer"],["Integer"],["Integer","Integer"],["Integer","Integer","Integer"],["String"],["Integer"]]isVariadic = [false,true,true,true,false,false,false]callArgumentTypes = ["String","Integer","Integer"]return = ["FuncA","FuncB"]FuncA is an exact three-parameter match. FuncB also matches because its final Integer parameter appears twice. Their names are returned in registration order.
Example 2
functionNames = ["FuncA","FuncB","FuncC","FuncD","FuncE","FuncF","FuncG"]functionArgumentTypes = [["String","Integer","Integer"],["String","Integer"],["Integer"],["Integer","Integer"],["Integer","Integer","Integer"],["String"],["Integer"]]isVariadic = [false,true,true,true,false,false,false]callArgumentTypes = ["Integer"]return = ["FuncC","FuncG"]FuncC is variadic and its required final Integer appears once. FuncG is the exact one-parameter match.
Example 3
functionNames = ["FuncA","FuncB","FuncC","FuncD","FuncE","FuncF","FuncG"]functionArgumentTypes = [["String","Integer","Integer"],["String","Integer"],["Integer"],["Integer","Integer"],["Integer","Integer","Integer"],["String"],["Integer"]]isVariadic = [false,true,true,true,false,false,false]callArgumentTypes = ["String"]return = ["FuncF"]FuncF is the only exact match. FuncB does not match because a variadic final parameter must occur at least once.
Constraints
1 <= functionNames.length == functionArgumentTypes.length == isVariadic.length <= 20000.- Function names are unique non-empty ASCII strings of at most
50characters. 1 <= functionArgumentTypes[i].length, and the total number of declared type tokens is at most200000.0 <= callArgumentTypes.length <= 200000.- Every type token is a non-empty ASCII string of at most
50characters. - Different functions may have identical signatures and flags; every matching name must still be returned.