A distinct string is a string that is present only once in an array.
Given an array of strings arr, and an integer k, return the kth distinct string present in arr. If there are fewer than k distinct strings, return an empty string " ".
Note that the strings are considered in the order in which they appear in the array.
Example 1:
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.
Example 2:
Input: arr = [ "aaa ", "aa ", "a "], k = 1 Output: "aaa " Explanation: All strings in arr are distinct, so the 1st string "aaa " is returned.
Example 3:
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 " ".
Constraints:
1 <= k <= arr.length <= 10001 <= arr[i].length <= 5arr[i] consists of lowercase English letters.program main
implicit none
character(len=:), allocatable :: arr(:)
integer :: k
character(len=:), allocatable :: result
! Examples
allocate(arr(6))
arr = ["d ", "b ", "c ", "b ", "c ", "a "]
k = 2
result = getKthDistinctString(arr, k)
print '(A)', result
allocate(arr(3))
arr = ["aaa ", "aa ", "a "]
k = 1
result = getKthDistinctString(arr, k)
print '(A)', result
allocate(arr(3))
arr = ["a ", "b ", "a "]
k = 3
result = getKthDistinctString(arr, k)
print '(A)', result
contains
function getKthDistinctString(arr, k) result(res)
implicit none
character(len=*), intent(in) :: arr(:)
integer, intent(in) :: k
character(len=:), allocatable :: res
logical, dimension(:), allocatable :: seen
integer :: i, j, n
n = size(arr)
allocate(seen(n))
seen = .false.
do i = 1, n
if (.not. seen(i)) then
do j = i + 1, n
if (arr(j) == arr(i)) then
seen(j) = .true.
end if
end do
end if
end do
if (count(seen) < k) then
res = ""
else
res = arr(k)
end if
end function getKthDistinctString
end program main
temp.f95:9:13:
9 | allocate(arr(6))
| 1
Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag
temp.f95:15:13:
15 | allocate(arr(3))
| 1
Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag
temp.f95:21:13:
21 | allocate(arr(3))
| 1
Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag
temp.f95:16:18:
16 | arr = ["aaa ", "aa ", "a "]
| 1
Error: Different CHARACTER lengths (4/3) in array constructor at (1)
program distinct_string
implicit none
integer, parameter :: k = 2
character(len=5), dimension(6) :: arr = ["d ", "b ", "c ", "b ", "c ", "a "]
character(len=5) :: result
integer :: i, count
! Initialize result to an empty string
result = " "
! Count the number of distinct strings in arr
count = 0
do i = 1, size(arr)
if (count(arr == arr(i)) == 1) then
count = count + 1
end if
end do
! If there are fewer than k distinct strings, return an empty string
if (count < k) then
result = " "
else
! Find the kth distinct string in arr
count = 0
do i = 1, size(arr)
if (count(arr == arr(i)) == 1) then
count = count + 1
if (count == k) then
result = arr(i)
exit
end if
end if
end do
end if
! Print the result
write (*,*) result
end program distinct_string
temp.f95:15:20:
15 | if (count(arr == arr(i)) == 1) then
| 1
Error: Syntax error in IF-expression at (1)
temp.f95:17:13:
17 | end if
| 1
Error: Expecting END DO statement at (1)
temp.f95:27:24:
27 | if (count(arr == arr(i)) == 1) then
| 1
Error: Syntax error in IF-expression at (1)
temp.f95:33:17:
33 | end if
| 1
Error: Expecting END DO statement at (1)
def is_good_string(s: str) -> bool:
freq_map = {}
for c in s:
if c in freq_map:
freq_map[c] += 1
else:
freq_map[c] = 1
count = next(iter(freq_map.values()))
for value in freq_map.values():
if value != count:
return False
return True
The algorithm for each implementation is as follows:
This algorithm works in O(n) time complexity, where n is the length of the input string, since it traverses the input string and dictionary in linear time.
#include <string>
#include <unordered_map>
bool isGoodString(const std::string& s) {
std::unordered_map<char, int> freq_map;
for (char c : s) {
freq_map[c]++;
}
int count = freq_map.begin()->second;
for (const auto& entry : freq_map) {
if (entry.second != count) {
return false;
}
}
return true;
}
The algorithm for each implementation is as follows:
This algorithm works in O(n) time complexity, where n is the length of the input string, since it traverses the input string and dictionary in linear time.