Leetcode 2053: Kth Distinct String in an Array

grid47
grid47
Exploring patterns and algorithms
Apr 15, 2024 4 min read

Find the k-th unique string in an array based on its first occurrence. If fewer than k unique strings exist, return an empty string.
Problem
Approach
Steps
Complexity
Input: An array of strings and an integer k.
Example: Input: arr = ["apple", "banana", "cherry", "banana", "apple", "date"], k = 2
Constraints:
• 1 <= k <= arr.length <= 1000
• 1 <= arr[i].length <= 5
• arr[i] consists of lowercase English letters.
Output: The k-th unique string based on order of appearance, or an empty string if fewer than k unique strings exist.
Example: Output: "date" for the given input arr and k.
Constraints:
Goal: Identify and return the k-th unique string.
Steps:
• Iterate through the array and count the occurrences of each string.
• Filter the strings that appear only once, maintaining their original order.
• Return the k-th unique string if it exists; otherwise, return an empty string.
Goal: Conditions that must be met by the inputs.
Steps:
• 1 <= k <= arr.length <= 1000
• 1 <= arr[i].length <= 5
• arr[i] consists of lowercase English letters.
Assumptions:
• All elements in the array are strings.
• Strings are considered case-sensitive.
• The array may contain duplicate strings.
Input: arr = ["apple", "banana", "cherry", "banana", "apple", "date"], k = 2
Explanation: Unique strings are ["cherry", "date"]. The 2nd unique string is "date".

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus