You are given a string s
and an integer array indices
of the same length. The string s
will be shuffled such that the character at the ith
position moves to indices[i]
in the shuffled string.
Return the shuffled string.
Example 1:
Input: s = "codeleet ", indices
= [4,5,6,7,0,2,1,3]
Output: "leetcode "
Explanation: As shown, "codeleet " becomes "leetcode " after shuffling.
Example 2:
Input: s = "abc ", indices
= [0,1,2]
Output: "abc "
Explanation: After shuffling, each character remains in its position.
Constraints:
s.length == indices.length == n
1 <= n <= 100
s
consists of only lowercase English letters.0 <= indices[i] < n
indices
are unique.program main
implicit none
character(len=*), parameter :: s = "codeleet "
integer, parameter :: n = len_trim(s)
integer, parameter :: indices(n) = [4,5,6,7,0,2,1,3]
character(len=n) :: shuffled_string
call solve(s, indices, shuffled_string)
print '(A)', trim(shuffled_string)
contains
subroutine solve(s, indices, shuffled_string)
implicit none
character(len=*), intent(in) :: s
integer, intent(in) :: indices(:)
character(len=*), intent(out) :: shuffled_string
integer :: i, j
do i = 1, size(indices)
j = indices(i)
if (j /= i) then
shuffled_string(j:j) = s(i:i)
end if
end do
end subroutine solve
end program main
eetcode^
program shuffle_string
implicit none
integer, parameter :: string_length = 100
character(len=string_length) :: s
integer, dimension(string_length) :: indices
integer :: i, j, n
! Read input
read(*,*) s
read(*,*) indices
! Shuffle the string
do i = 1, n
j = indices(i)
if (i /= j) then
s(j:j) = s(i:i)
s(i:i) = ' '
end if
end do
! Print output
write(*,*) s
end program shuffle_string
At line 10 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7b6c01126960 in ??? #1 0x7b6c011274d9 in ??? #2 0x7b6c0137b17b in ??? #3 0x7b6c01374684 in ??? #4 0x7b6c013752aa in ??? #5 0x5b465f2fd21c in MAIN__ #6 0x5b465f2fd410 in main
from typing import List
def kids_with_candies(candies: List[int], extra_candies: int) -> List[bool]:
max_candies = max(candies)
result = [candy + extra_candies >= max_candies for candy in candies]
return result
The algorithm first finds the maximum number of candies any kid has by iterating over the candies array. Then, it iterates over the candies array again, checking if the number of candies a particular kid has plus the extra candies is greater than or equal to the maximum number of candies. If it is, it adds true
to the result array, otherwise false
. This result array is returned. The algorithm has a time complexity of O(n), where n is the length of the candies array.
#include <vector>
using namespace std;
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
int max_candies = *max_element(candies.begin(), candies.end());
vector<bool> result(candies.size());
for (int i = 0; i < candies.size(); i++) {
result[i] = (candies[i] + extraCandies >= max_candies);
}
return result;
}
The algorithm first finds the maximum number of candies any kid has by iterating over the candies array. Then, it iterates over the candies array again, checking if the number of candies a particular kid has plus the extra candies is greater than or equal to the maximum number of candies. If it is, it adds true
to the result array, otherwise false
. This result array is returned. The algorithm has a time complexity of O(n), where n is the length of the candies array.