Skip to content

Latest commit

 

History

History
56 lines (39 loc) · 1.25 KB

_2053. Kth Distinct String in an Array.md

File metadata and controls

56 lines (39 loc) · 1.25 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : August 05, 2024

Last updated : August 05, 2024


Related Topics : Array, Hash Table, String, Counting

Acceptance Rate : 81.99 %


Solutions

Python

class Solution:
    def kthDistinct(self, arr: List[str], k: int) -> str:
        distinct = set()
        notdistinct = set()

        for s in arr :
            if s in notdistinct :
                continue
            if s in distinct :
                distinct.remove(s)
                notdistinct.add(s)
            else :
                distinct.add(s)

        
        if k > len(distinct) :
            return ""
        
        for x in arr :
            if x not in distinct :
                continue
            k -= 1

            if k <= 0 :
                return x
        
        return 'error'