Problem · Array
Ordered IP Firewall
Learn this problemProblem statement
You are given an ordered IPv4 firewall. Rule i has action actions[i], which is either "ALLOW" or "DENY", and target cidrs[i], which is either one IPv4 address or an IPv4 CIDR network.
Evaluate the query address against the rules in order. A plain address matches only itself. A CIDR target a.b.c.d/p matches exactly the addresses whose first p bits equal the target network's first p bits. Return true for the first matching ALLOW rule and false for the first matching DENY rule.
Function
allowAccess(actions: String[], cidrs: String[], ip: String) → booleanExamples
Example 1
actions = ["DENY","ALLOW"]cidrs = ["10.0.0.0/8","0.0.0.0/0"]ip = "10.4.5.6"return = falseThe address is inside 10.0.0.0/8, so the first rule matches and denies access.
Example 2
actions = ["ALLOW","DENY","ALLOW"]cidrs = ["192.168.1.9","192.168.1.0/24","0.0.0.0/0"]ip = "192.168.1.9"return = trueThe exact-address rule appears before the broader deny rule, so its allow decision wins.
Constraints
1 <= actions.length = cidrs.length <= 200000.- Every action is
"ALLOW"or"DENY". - Every target and query is a valid dotted-decimal IPv4 address; a CIDR prefix is between
0and32. - At least one rule matches the query.