-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solutions and explanation of the day 2053
- Loading branch information
Aarzoo
committed
Aug 5, 2024
1 parent
2b303f2
commit 07467d9
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Algorithms/src/2053. Kth Distinct String in an Array/Code/solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <vector> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
class Solution | ||
{ | ||
public: | ||
string kthDistinct(std::vector<std::string> &arr, int k) | ||
{ | ||
// Create an unordered map to count occurrences of each string | ||
std::unordered_map<std::string, int> count; | ||
// Create a vector to store distinct strings | ||
std::vector<std::string> distinct; | ||
|
||
// Count occurrences of each string in the array | ||
for (const std::string &str : arr) | ||
{ | ||
count[str]++; | ||
} | ||
|
||
// Collect distinct strings (strings that appear exactly once) in order | ||
for (const std::string &str : arr) | ||
{ | ||
if (count[str] == 1) | ||
{ | ||
distinct.push_back(str); | ||
} | ||
} | ||
|
||
// If k is within the range of distinct strings, return the k-th distinct string | ||
// Otherwise, return an empty string | ||
if (k <= distinct.size()) | ||
{ | ||
return distinct[k - 1]; | ||
} | ||
else | ||
{ | ||
return ""; | ||
} | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
Algorithms/src/2053. Kth Distinct String in an Array/Code/solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
func kthDistinct(arr []string, k int) string { | ||
// Create a map to count the occurrences of each string | ||
count := make(map[string]int) | ||
|
||
// Create a slice to store distinct strings | ||
distinct := []string{} | ||
|
||
// Loop through each string in the array and count its occurrences | ||
for _, str := range arr { | ||
count[str]++ | ||
} | ||
|
||
// Loop through the array again to collect distinct strings in order | ||
for _, str := range arr { | ||
if count[str] == 1 { | ||
// If a string appears exactly once, add it to the distinct slice | ||
distinct = append(distinct, str) | ||
} | ||
} | ||
|
||
// Check if k is within the range of distinct strings | ||
if k <= len(distinct) { | ||
// Return the k-th distinct string (1-based index) | ||
return distinct[k-1] | ||
} else { | ||
// If k is out of range, return an empty string | ||
return "" | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Algorithms/src/2053. Kth Distinct String in an Array/Code/solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public String kthDistinct(String[] arr, int k) { | ||
// Create a HashMap to count occurrences of each string | ||
Map<String, Integer> count = new HashMap<>(); | ||
|
||
// Create a List to store distinct strings | ||
List<String> distinct = new ArrayList<>(); | ||
|
||
// Loop through each string in the array | ||
for (String str : arr) { | ||
// Increment the count for each string | ||
count.put(str, count.getOrDefault(str, 0) + 1); | ||
} | ||
|
||
// Loop through each string in the array again to collect distinct strings | ||
for (String str : arr) { | ||
// If the string appears only once, add it to the distinct list | ||
if (count.get(str) == 1) { | ||
distinct.add(str); | ||
} | ||
} | ||
|
||
// Check if the k-th distinct string exists | ||
if (k <= distinct.size()) { | ||
// Return the k-th distinct string (1-based index) | ||
return distinct.get(k - 1); | ||
} else { | ||
// If there are fewer than k distinct strings, return an empty string | ||
return ""; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Algorithms/src/2053. Kth Distinct String in an Array/Code/solution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Function to find the k-th distinct string in an array. | ||
* @param {string[]} arr - The array of strings. | ||
* @param {number} k - The position of the distinct string to find. | ||
* @return {string} - The k-th distinct string or an empty string if it doesn't exist. | ||
*/ | ||
var kthDistinct = function (arr, k) { | ||
let count = new Map(); // Create a map to count occurrences of each string | ||
let distinct = []; // Array to store distinct strings | ||
|
||
// Count occurrences of each string | ||
for (let str of arr) { | ||
count.set(str, (count.get(str) || 0) + 1); // Increment count for each string | ||
} | ||
|
||
// Collect distinct strings in order | ||
for (let str of arr) { | ||
if (count.get(str) === 1) { | ||
// Check if the string is distinct | ||
distinct.push(str); // Add distinct string to the array | ||
} | ||
} | ||
|
||
// Return the k-th distinct string or an empty string if it doesn't exist | ||
return k <= distinct.length ? distinct[k - 1] : ""; // Adjust index for 1-based k | ||
}; |
19 changes: 19 additions & 0 deletions
19
Algorithms/src/2053. Kth Distinct String in an Array/Code/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution: | ||
def kthDistinct(self, arr: List[str], k: int) -> str: | ||
count = {} # Dictionary to store the frequency of each string | ||
distinct = [] # List to store distinct strings | ||
|
||
# Iterate through each string in the array to count occurrences | ||
for str in arr: | ||
count[str] = count.get(str, 0) + 1 # Increment the count for each string | ||
|
||
# Iterate through the array again to collect distinct strings | ||
for str in arr: | ||
if count[str] == 1: # Check if the string is distinct (appears only once) | ||
distinct.append(str) # Add distinct string to the list | ||
|
||
# Check if the k-th distinct string exists | ||
if k <= len(distinct): | ||
return distinct[k-1] # Return the k-th distinct string (1-based index) | ||
else: | ||
return "" # Return an empty string if k is out of range |
58 changes: 58 additions & 0 deletions
58
Algorithms/src/2053. Kth Distinct String in an Array/Explanation/explanation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Finding the K-th Distinct String in an Array | ||
|
||
This README provides a step-by-step explanation of how to find the k-th distinct string in an array using different programming languages: C++, Java, JavaScript, Python, and Go. | ||
|
||
## C++ Code | ||
|
||
### Step-by-Step Explanation | ||
|
||
1. **Create an unordered map** to count the occurrences of each string in the array. | ||
2. **Create a vector** to store distinct strings. | ||
3. **Count occurrences** of each string by iterating through the array. | ||
4. **Collect distinct strings** by iterating through the array again and checking if the count of each string is exactly one. | ||
5. **Check if k** is within the range of distinct strings. | ||
6. **Return the k-th distinct string** if it exists, otherwise return an empty string. | ||
|
||
## Java Code | ||
|
||
### Step-by-Step Explanation | ||
|
||
1. **Create a HashMap** to count the occurrences of each string. | ||
2. **Create a List** to store distinct strings. | ||
3. **Loop through the array** to count occurrences of each string, using the `getOrDefault` method to handle new strings. | ||
4. **Loop through the array again** to collect distinct strings by checking if the count of each string is exactly one. | ||
5. **Check if the k-th distinct string** exists by comparing k with the size of the distinct list. | ||
6. **Return the k-th distinct string** if it exists, otherwise return an empty string. | ||
|
||
## JavaScript Code | ||
|
||
### Step-by-Step Explanation | ||
|
||
1. **Create a Map** to count the occurrences of each string. | ||
2. **Create an array** to store distinct strings. | ||
3. **Count occurrences** of each string by iterating through the array and updating the Map. | ||
4. **Collect distinct strings** by iterating through the array again and checking if the count of each string is exactly one. | ||
5. **Check if k** is within the range of distinct strings. | ||
6. **Return the k-th distinct string** if it exists, otherwise return an empty string. | ||
|
||
## Python Code | ||
|
||
### Step-by-Step Explanation | ||
|
||
1. **Create a dictionary** to store the frequency of each string. | ||
2. **Create a list** to store distinct strings. | ||
3. **Iterate through the array** to count occurrences of each string, using the `get` method to handle new strings. | ||
4. **Iterate through the array again** to collect distinct strings by checking if the count of each string is exactly one. | ||
5. **Check if the k-th distinct string** exists by comparing k with the length of the distinct list. | ||
6. **Return the k-th distinct string** if it exists, otherwise return an empty string. | ||
|
||
## Go Code | ||
|
||
### Step-by-Step Explanation | ||
|
||
1. **Create a map** to count the occurrences of each string. | ||
2. **Create a slice** to store distinct strings. | ||
3. **Loop through the array** to count occurrences of each string. | ||
4. **Loop through the array again** to collect distinct strings by checking if the count of each string is exactly one. | ||
5. **Check if k** is within the range of distinct strings. | ||
6. **Return the k-th distinct string** if it exists, otherwise return an empty string. |