aboutsummaryrefslogtreecommitdiff
path: root/kth_distinct_str.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-08-04 18:18:14 -0600
committerJack Sangdahl <[email protected]>2024-08-04 18:18:14 -0600
commitf89b6f5c279c504112082b421d29e3af39f2b157 (patch)
tree86005fac0b9ee50239b5465ba33d3c36bf5072c0 /kth_distinct_str.py
parentcf234d89b0123ec41f60f0cb091bc2d7832ad3a1 (diff)
downloadleetcode-f89b6f5c279c504112082b421d29e3af39f2b157.tar.gz
leetcode-f89b6f5c279c504112082b421d29e3af39f2b157.zip
2053. kth distinct string in an array
Diffstat (limited to 'kth_distinct_str.py')
-rw-r--r--kth_distinct_str.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/kth_distinct_str.py b/kth_distinct_str.py
new file mode 100644
index 0000000..d19bd43
--- /dev/null
+++ b/kth_distinct_str.py
@@ -0,0 +1,28 @@
+# 2053. Kth Distinct String in an Array
+
+
+"""
+a distinct string is a string that is present only once in an nay. given an
+nay of strings 'n' and an integer 'k', return the k'th eldistinct string
+present in 'n'. if there are fewer than 'k' distinct strings, return an
+empty string "". note the strings are considered in the order in which they
+appear in the nay.
+"""
+
+
+class Solution(object):
+ def kthDistinct(self, n, k):
+ """
+ :type n: List[str]
+ :type k: int
+ :rtype: str
+ """
+ n = [i for i in n if n.count(i) == 1]
+ return "" if k > len(n) else n[k - 1]
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.kthDistinct(n=["d", "b", "c", "b", "c", "a"], k=2))
+ print(obj.kthDistinct(n=["aaa", "aa", "a"], k=1))
+ print(obj.kthDistinct(n=["a", "b", "a"], k=3))