[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

21. Merge Two Sorted Lists

What The Heck Does Code Do?

Blog #9 Creating a Main Menu and Suffering Limitations

Event-driven architecture made easy with Apache NiFi

OpenCart PHP product trade continues going on — this time without CMS category prerequisites…

Django React App — From Scratch to Deployment Part-1

Rails to Heroku: A brief walkthrough for you

Nervos CKB Development Dossier #42

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 Patterns Adventure 7 — Maximum Subarray

[LeetCode]#2089. Find Target Indices After Sorting Array

Python TreeMap

[LeetCode]#1967. Number of Strings That Appear as Substrings in Word