Kth lexicographically smallest distinct String from given Array of Strings
Last Updated :
10 Dec, 2021
Given an array arr having N strings and an integer K, the task is to find the lexicographically smallest Kth distinct string. Print an empty string if no such string exists.
Example:
Input: arr[]={"aa", "aa", "bb", "cc", "dd", "cc"}, K = 2
Output: dd
Explanation: Distinct strings are: "bb", "dd". 2nd smallest string among these is "dd"
Input: arr[]={"aa", "aa", "bb", "cc", "dd", "cc"}, K = 1
Output: bb
Approach: The given problem can be solved by first sorting the given array of strings, and then printing the Kth string with frequency 1. Follow the below steps to solve this problem:
- Sort the given array of strings
- Create a map to store the frequency of each string.
- Now, traverse the map and reduce the value of K each time a string having a frequency of one is found.
- When K becomes zero, print the next string having a frequency of 1.
Below is the implementation of the above approach:
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print lexicographically
// smallest Kth string
string KthDistinctString(vector<string>& arr, int K)
{
// Sorting the array of strings
sort(arr.begin(), arr.end());
// Map to store the strings
map<string, int> mp;
for (auto x : arr) {
mp[x]++;
}
for (auto x : mp) {
// Reducing K
if (x.second == 1) {
K--;
}
if (K == 0 and x.second == 1) {
return x.first;
}
}
return "";
}
// Driver Code
int main()
{
vector<string> a
= { "aa", "aa", "bb", "cc", "dd", "cc" };
int K = 2;
cout << KthDistinctString(a, K);
}
Java
// Java code for the above approach
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
class GFG {
// Function to print lexicographically
// smallest Kth string
static String KthDistinctString(ArrayList<String> arr, int K) {
// Sorting the array of strings
Collections.sort(arr);
// Map to store the strings
HashMap<String, Integer> mp = new HashMap<String, Integer>();
for (String x : arr) {
int count = 0;
if (mp.containsKey(x)) {
count = mp.get(x);
}
mp.put(x, count + 1);
}
for (String x : mp.keySet()) {
// Reducing K
if (mp.get(x) == 1) {
K--;
}
if (K == 0 && mp.get(x) == 1) {
return x;
}
}
return "";
}
// Driver Code
public static void main(String args[]) {
ArrayList<String> a = new ArrayList<String>();
a.add("aa");
a.add("aa");
a.add("bb");
a.add("cc");
a.add("dd");
a.add("cc");
int K = 2;
System.out.println(KthDistinctString(a, K));
}
}
// This code is contributed by gfgking
Python3
# Python3 code for the above approach
# Function to print lexicographically
# smallest Kth string
def KthDistinctString(arr, K):
# Sorting the array of strings
arr.sort()
# Map to store the strings
mp = {}
for x in arr:
if x in mp:
mp[x] += 1
else:
mp[x] = 1
for x in mp:
# Reducing K
if (mp[x] == 1):
K -= 1
if (K == 0 and mp[x] == 1):
return x
return ""
# Driver Code
if __name__ == "__main__":
a = [ "aa", "aa", "bb", "cc", "dd", "cc" ]
K = 2
print(KthDistinctString(a, K))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function to print lexicographically
// smallest Kth string
static string KthDistinctString(ArrayList arr, int K)
{
// Sorting the array of strings
arr.Sort();
// Map to store the strings
Dictionary<string, int> mp =
new Dictionary<string, int>();
foreach (string x in arr) {
int count = 0;
if (mp.ContainsKey(x)) {
count = mp[x];
}
mp[x] = count + 1;
}
foreach (KeyValuePair<string, int> x in mp) {
// Reducing K
if (x.Value == 1) {
K--;
}
if (K == 0 && x.Value == 1) {
return x.Key;
}
}
return "";
}
// Driver Code
public static void Main()
{
ArrayList a = new ArrayList();
a.Add("aa");
a.Add("aa");
a.Add("bb");
a.Add("cc");
a.Add("dd");
a.Add("cc");
int K = 2;
Console.Write(KthDistinctString(a, K));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to print lexicographically
// smallest Kth string
function KthDistinctString(arr, K) {
// Sorting the array of strings
arr.sort();
// Map to store the strings
let mp = new Map();
for (let x of arr) {
if (mp.has(x))
mp.set(x, mp.get(x) + 1);
else
mp.set(x, 1);
}
for (let [key, value] of mp)
{
// Reducing K
if (value == 1) {
K--;
}
if (K == 0 && value == 1) {
return key;
}
}
return "";
}
// Driver Code
let a
= ["aa", "aa", "bb", "cc", "dd", "cc"];
let K = 2;
document.write(KthDistinctString(a, K));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Similar Reads
Lexicographically smallest string which differs from given strings at exactly K indices Given two strings S1 and S2 of length N and a positive integer K, the task is to find the lexicographically smallest string such that it differs from the given two strings S1 and S2 at exactly K places. If there is no such string exists then print "-1". Examples: Input: N = 4, K = 3, S1 = "ccbb", S2
15 min read
Lexicographically smallest string whose hamming distance from given string is exactly K Given a lowercase string A of length N and an integer K, find the lexicographically smallest string B of the same length as A such that hamming distance between A and B is exactly K. Examples: Input : A = "pqrs", k = 1. Output : aqrs We can differ by at most one character. So we put 'a' in the begin
8 min read
K-th lexicographically smallest unique substring of a given string Given a string S. The task is to print the K-th lexicographically the smallest one among the different substrings of s.A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z, and an empty stri
5 min read
Lexicographically smallest string with given string as prefix Given an array arr[] consisting of N strings and a string S if size M, the task is to find the lexicographically smallest string consisting of the string S as the prefix. If there doesn't exist any string starting with prefix S then print "-1". Examples: Input: arr[] = {"apple", "appe", "apl", "aapl
6 min read
Lexicographically smallest K-length subsequence from a given string Given a string s of length n, the task is to find the lexicographically smallest k-length subsequence from the string s (where k < n). Examples:Input: s = "bbcaab", k = 3Output: "aab"Input: s = "aabdaabc", k = 3Output: "aaa"[Naive Approach] Generating all Subsequences - O(2^n) time and O(C(n,k)*k
9 min read
Lexicographically Kth-smallest string having 'a' X times and 'b' Y times Given three non-negative integers, X, Y, and K, the task is to find the Kth smallest lexicographical string having X occurrences of character 'a' and Y occurrences of character 'b'. Examples: Input: X = 2, Y = 3, K = 3Output: abbabExplanation: First lexicographical smallest string = "aabbb".Second l
15+ min read