> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Kth Distinct String in an Array

> Tested Python solution for LeetCode 2053 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 2053, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/kth-distinct-string-in-an-array/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2053   # by problem number
lcpy gen -s kth_distinct_string_in_an_array   # by problem name
```

## Problem

A \<strong>distinct string\</strong> is a string that is present only \<strong>once\</strong> in an array.

Given an array of strings \<code>arr\</code>, and an integer \<code>k\</code>, return \<em>the \</em>\<code>k\<sup>th\</sup>\</code>\<em> \<strong>distinct string\</strong> present in \</em>\<code>arr\</code>. If there are \<strong>fewer\</strong> than \<code>k\</code> distinct strings, return \<em>an \<strong>empty string \</strong>\</em>\<code>""\</code>.

Note that the strings are considered in the \<strong>order in which they appear\</strong> in the array.

### Examples

```
Input: arr = ["d","b","c","b","c","a"], k = 2
Output: "a"
Explanation:
The only distinct strings in arr are "d" and "a".
"d" appears 1st, so it is the 1st distinct string.
"a" appears 2nd, so it is the 2nd distinct string.
Since k == 2, "a" is returned.
```

```
Input: arr = ["aaa","aa","a"], k = 1
Output: "aaa"
Explanation:
All strings in arr are distinct, so the 1st string "aaa" is returned.
```

```
Input: arr = ["a","b","a"], k = 3
Output: ""
Explanation:
The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
```

### Constraints

* `1 <= k <= arr.length <= 1000`
* `1 <= arr[i].length <= 5`
* `arr[i]` consists of lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/kth_distinct_string_in_an_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/kth_distinct_string_in_an_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n)
    # Space: O(n)
    def kth_distinct(self, arr: list[str], k: int) -> str:
        counts = Counter(arr)
        for s in arr:
            if counts[s] == 1:
                k -= 1
                if k == 0:
                    return s
        return ""
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.