[LeetCode]#2053. Kth Distinct String in an Array

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 "".
  1. Counter arr
  2. Find only distinct strings
  3. return the order is match k
from collections import Counter
class Solution:
def kthDistinct(self, arr, k):
cnt=Counter(arr)
for key,v in cnt.items():
if v==1:
k-=1
if k==0:
return key
return ""

--

--

--

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Day 2 — What is StatelessWidget and an example of how it works

Sum of two elements (2sum)

Photo: Two zebras

Product Update Issue 7: C-OP Beta Test and ULend Capital Efficiency

nitCreating Collectables in Unity

C- Programming: Data Types, Variable and Constants

Run the colors

Combining repository pattern and unit of work using EntityFramework Core

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2114. Maximum Number of Words Found in Sentences

Python Interview Questions for 2022

Implementing Linked List Operations in Python

LeetCode Patterns Adventure 12 — Palindrome Linked List