PracticeArrays

Count Matching Archive Records

BeginnerArraystraversalaccumulationstate tracking Time not estimated Not started

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: 2
Explanation: The function returns 2 for the first example.
Example 2
Input: records = ["invoice 001", "invoice 002", "invoice 003"]target = "invoice 004"
Output: 0
Explanation: The function returns 0 for the second example.
Example 3
Input: records = ["ready", " ready", "ready", "done"]target = "ready"
Output: 2
Explanation: The function returns 2 for the third example.

Constraints

  • 0 <= len(records) <= 100000
  • 0 <= len(records[i]) <= 200 for every i
  • 0 <= len(target) <= 200
  • Records and target may contain spaces and other characters
  • compare complete strings without trimming or altering them.
  • Types: records is str[], target is str; result is int

Function signature

def count_matching_records(records: list[str], target: str) -> int
records list[str]
The file records in their original order.
target str
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.

Approved · t2-abs_658_v414 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1