You're given strings jewels
representing the types of stones that are jewels, and stones
representing the stones you have. Each character in stones
is a type of stone you have. You want to know how many of the stones you have are also jewels.
Letters are case sensitive, so "a "
is considered a different type of stone from "A "
.
Example 1:
Input: jewels = "aA", stones = "aAAbbbb" Output: 3
Example 2:
Input: jewels = "z", stones = "ZZ" Output: 0
Constraints:
1 <= jewels.length, stones.length <= 50
jewels
and stones
consist of only English letters.jewels
are unique.program main
implicit none
integer :: i, n_jewels, n_stones
character(len=50) :: jewels, stones
! Example 1
jewels = "aA"
stones = "aAAbbbb"
call solve(jewels, stones, n_jewels, n_stones)
write(*,"(A,I0)") "Example 1: ", n_jewels
! Example 2
jewels = "z"
stones = "ZZ"
call solve(jewels, stones, n_jewels, n_stones)
write(*,"(A,I0)") "Example 2: ", n_jewels
contains
subroutine solve(jewels, stones, n_jewels, n_stones)
implicit none
character(len=*), intent(in) :: jewels, stones
integer, intent(out) :: n_jewels, n_stones
integer :: i, j
n_jewels = 0
n_stones = len(stones)
do i = 1, len(jewels)
do j = 1, len(stones)
if (jewels(i:i) == stones(j:j)) then
n_jewels = n_jewels + 1
exit
end if
end do
end do
end subroutine solve
end program main
Example 1: 50 Example 2: 49
module Main
implicit none
contains
subroutine solve(jewels, stones, n)
character(len=*), intent(in) :: jewels, stones
integer, intent(out) :: n
integer :: i, j
n = 0
do i = 1, len(stones)
do j = 1, len(jewels)
if (stones(i:i) == jewels(j:j)) then
n = n + 1
exit
end if
end do
end do
end subroutine solve
end module Main
program main
use Main
implicit none
character(len=50) :: jewels, stones
integer :: n
! Example 1
jewels = "aA"
stones = "aAAbbbb"
call solve(jewels, stones, n)
write (*,*) "Example 1: ", n
! Example 2
jewels = "z"
stones = "ZZ"
call solve(jewels, stones, n)
write (*,*) "Example 2: ", n
end program main
temp.f95:21:12: 1 | module Main | 2 ...... 21 | program main | 1 Error: Global name βmainβ at (1) is already being used as a MODULE at (2) temp.f95:22:7: 22 | use Main | 1 Error: βmainβ of module βmainβ, imported at (1), is also the name of the current program unit
def num_jewels_in_stones(jewels: str, stones: str) -> int:
count = 0
for stone in stones:
if stone in jewels:
count += 1
return count
The algorithm is pretty simple, as it iterates through each character (stone) in the stones
string, and checks if that character is present in the jewels
string. If it is, increment the counter. Finally, return the counter as the result.
This method does not require any additional space, except for the counter variable, and its time complexity is O(n*m) where n is the length of stones
and m is the length of jewels
.
int numJewelsInStones(string jewels, string stones) {
int count = 0;
for (char stone : stones) {
if (jewels.find(stone) != std::string::npos) {
count++;
}
}
return count;
}
The algorithm is pretty simple, as it iterates through each character (stone) in the stones
string, and checks if that character is present in the jewels
string. If it is, increment the counter. Finally, return the counter as the result.
This method does not require any additional space, except for the counter variable, and its time complexity is O(n*m) where n is the length of stones
and m is the length of jewels
.