Given an array of strings passwords and an integer k, filter out any password that appears k or more times in the array.
Return the list of accepted passwords (those appearing fewer than k times). The result should maintain the order of first appearance of each password, and each accepted password should appear only once in the output.
Function Signature
def filter_passwords(passwords: list[str], k: int) -> list[str]:
Rules
-
Two entries are the same password only when the strings are identical, character for character.
-
A password that appears exactly
k
times is rejected.
-
Accepted passwords are ordered by the index of their first occurrence in
passwords
. If nothing is accepted, return an empty list.
Constraints
-
1 <= passwords.length <= 10^5
-
1 <= passwords[i].length <= 100
-
1 <= k <= 10^5
-
passwords[i]
consists of lowercase English letters, digits, and special characters.
Examples
Input: passwords = ["abc","def","abc","ghi","abc","def"], k = 3
Output: ["def","ghi"]
"abc" appears 3 times, which reaches k = 3, so it is rejected. "def" appears 2 times and "ghi" once, so both are accepted, in the order they first appear (index 1, then index 3), and "def" is listed once.
Input: passwords = ["pass1","pass2","pass1","pass2","pass3"], k = 2
Output: ["pass3"]
"pass1" and "pass2" each appear 2 times, which reaches k = 2, so both are rejected. "pass3" appears once and is the only accepted password.
Input: passwords = ["q1","q2","q1"], k = 1
Output: []
With k = 1, every password appears at least once, so every password is rejected and the result is empty.