PracticeArrays
Count Matching Archive Records
Context
A file auditor reviews a batch of exported record lines and wants to know how many lines exactly match a flagged record label. Each line is treated as one complete string, including any spaces or other characters it contains.
Problem
Implement a function that counts the records in a file batch that are exactly equal to the requested target string. Examine the records from left to right, add one to the total whenever the current record equals the target, and return the total after every record has been examined. Matching records should be counted independently, including repeated occurrences. An empty batch has no matches.
Examples
Example 1
Input:
records = ["alpha", "beta", "alpha", "gamma"]target = "alpha"Output:
2Explanation: The function returns 2 for the first example.
Example 2
Input:
records = ["invoice 001", "invoice 002", "invoice 003"]target = "invoice 004"Output:
0Explanation: The function returns 0 for the second example.
Example 3
Input:
records = ["ready", " ready", "ready", "done"]target = "ready"Output:
2Explanation: The function returns 2 for the third example.
Constraints
0 <= len(records) <= 1000000 <= len(records[i]) <= 200 for every i0 <= len(target) <= 200Records and target may contain spaces and other characterscompare complete strings without trimming or altering them.- Types:
recordsis str[],targetis str; result is int
Function signature
def count_matching_records(records: list[str], target: str) -> int
recordslist[str]- The file records in their original order.
targetstr- The complete record string to count.
- returns int
- The number of records exactly equal to target.
Adapted from MBPP problem task_204 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Code
Saved