Problem · Array
Match a Variadic Function Signature
Learn this problemProblem statement
You are given the declared parameter type tokens of one function in parameterTypes, a flag isVariadicFunction, and the ordered argument type tokens of one call in argumentTypes.
Return true if the call matches the declared signature. Otherwise, return false.
Matching rules
- Type tokens are opaque strings. Two tokens match only when they are exactly equal, including case. There is no trimming, aliasing, coercion, subtyping, or wildcard behavior.
- When
isVariadicFunctionisfalse, the call must have exactly the same number of arguments as the signature, and every token must match at the same position. - When
isVariadicFunctionistrue,parameterTypesis non-empty. Its final token is the variadic type, while all earlier tokens form the fixed prefix. - The variadic type may occur zero or more times. Therefore, a variadic signature with
mdeclared tokens accepts calls with at leastm - 1arguments. - Every argument after the fixed prefix must exactly equal the final declared variadic token.
Function
matchesFunctionSignature(parameterTypes: String[], isVariadicFunction: boolean, argumentTypes: String[]) → booleanExamples
Example 1
parameterTypes = ["String","Integer"]isVariadicFunction = falseargumentTypes = ["String","Integer"]return = trueThis is a fixed signature. The call has the same arity, and both type tokens match at their positions.
Example 2
parameterTypes = ["String","Integer"]isVariadicFunction = trueargumentTypes = ["String","Integer","Integer","Integer"]return = trueString is the fixed prefix. Every remaining argument exactly matches the variadic type Integer.
Example 3
parameterTypes = ["String","Integer"]isVariadicFunction = trueargumentTypes = ["String"]return = trueThe fixed prefix matches, and the final variadic type is allowed to occur zero times.
Example 4
parameterTypes = ["String","Integer"]isVariadicFunction = trueargumentTypes = ["String","Integer","Double"]return = falseThe last argument is Double, which does not exactly match the variadic type Integer.
Constraints
0 <= parameterTypes.length <= 10^50 <= argumentTypes.length <= 10^5- If
isVariadicFunctionistrue, thenparameterTypes.length >= 1. - Every type token is a non-empty ASCII string of at most
100characters.