A string s
can be partitioned into groups of size k
using the following procedure:
k
characters of the string, the second group consists of the next k
characters of the string, and so on. Each character can be a part of exactly one group.k
characters remaining, a character fill
is used to complete the group.Note that the partition is done so that after removing the fill
character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be s
.
Given the string s
, the size of each group k
and the character fill
, return a string array denoting the composition of every group s
has been divided into, using the above procedure.
Example 1:
Input: s = "abcdefghi ", k = 3, fill = "x " Output: [ "abc ", "def ", "ghi "] Explanation: The first 3 characters "abc " form the first group. The next 3 characters "def " form the second group. The last 3 characters "ghi " form the third group. Since all groups can be completely filled by characters from the string, we do not need to use fill. Thus, the groups formed are "abc ", "def ", and "ghi ".
Example 2:
Input: s = "abcdefghij ", k = 3, fill = "x " Output: [ "abc ", "def ", "ghi ", "jxx "] Explanation: Similar to the previous example, we are forming the first three groups "abc ", "def ", and "ghi ". For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice. Thus, the 4 groups formed are "abc ", "def ", "ghi ", and "jxx ".
Constraints:
1 <= s.length <= 100
s
consists of lowercase English letters only.1 <= k <= 100
fill
is a lowercase English letter.program main
implicit none
integer :: i, j, k
character(len=100) :: s
character(len=100) :: fill
character(len=100), allocatable :: groups(:)
! Example 1
s = "abcdefghi "
k = 3
fill = "x "
call solve(s, k, fill, groups)
write(*,*) trim(groups(1)) // trim(groups(2)) // trim(groups(3))
! Example 2
s = "abcdefghij "
k = 3
fill = "x "
call solve(s, k, fill, groups)
write(*,*) trim(groups(1)) // trim(groups(2)) // trim(groups(3)) // trim(groups(4))
contains
subroutine solve(s, k, fill, groups)
implicit none
character(len=*), intent(in) :: s
integer, intent(in) :: k
character(len=*), intent(in) :: fill
character(len=*), allocatable, intent(out) :: groups(:)
integer :: n
integer :: i
n = ceiling(real(len(s)) / real(k))
allocate(groups(n))
do i = 1, n-1
groups(i) = s(1+((i-1)*k):k*(i))
end do
if (mod(len(s), k) == 0) then
groups(n) = s(1+(n-1)*k:len(s))
else
groups(n) = fill(1:(len(s)-(n-1)*k)) // s(1+(n-1)*k:len(s))
end if
end subroutine solve
end program main
abcdefghi abcdefghij
program main
implicit none
integer, parameter :: k = 3
character(len=100), parameter :: fill = "x "
character(len=100) :: s
character(len=k), dimension(:), allocatable :: groups
read(*,*) s
allocate(groups(ceiling(real(len(s))/k)))
groups = [character(len=k) :: ]
do i = 1, ceiling(real(len(s))/k)
groups(i) = s(i*k-k+1:i*k)
end do
if (mod(len(s),k) /= 0) then
groups(ceiling(real(len(s))/k)) = fill(1:k-mod(len(s),k)) // groups(ceiling(real(len(s))/k))
end if
write(*,*) groups
end program main
temp.f95:16:10: 16 | do i = 1, ceiling(real(len(s))/k) | 1 Error: Symbol āiā at (1) has no IMPLICIT type
def sum_of_beauties(nums: List[int]) -> int:
n = len(nums)
result = 0
left_max = [0] * n
left_max[0] = nums[0]
right_min = [0] * n
right_min[n - 1] = nums[n - 1]
for i in range(1, n):
left_max[i] = max(left_max[i - 1], nums[i])
for i in range(n - 2, -1, -1):
right_min[i] = min(right_min[i + 1], nums[i])
for i in range(1, n - 1):
if nums[i] > left_max[i - 1] and nums[i] < right_min[i + 1]:
result += 1
return result
To solve this problem, we first create two arrays, leftMax
and rightMin
. leftMax
stores the maximum value to the left of each index i, and rightMin
stores the minimum value to the right of each index i. We then iterate through the input array nums
from index 1 to index n - 2 (inclusive) and check if the current value is greater than the maximum to its left and less than the minimum to its right. If both conditions are fulfilled, we increase the result by 1. Finally, we return the result as the sum of the beauty of all nums[i]
where 1 <= i <= nums.length - 2.
int sumOfBeauties(std::vector<int>& nums) {
int n = nums.size();
int result = 0;
std::vector<int> leftMax(n);
leftMax[0] = nums[0];
std::vector<int> rightMin(n);
rightMin[n - 1] = nums[n - 1];
for (int i = 1; i < n; i++) {
leftMax[i] = std::max(leftMax[i - 1], nums[i]);
}
for (int i = n - 2; i >= 0; i--) {
rightMin[i] = std::min(rightMin[i + 1], nums[i]);
}
for (int i = 1; i <= n - 2; i++) {
if (nums[i] > leftMax[i - 1] && nums[i] < rightMin[i + 1]) {
result += 1;
}
}
return result;
}
To solve this problem, we first create two arrays, leftMax
and rightMin
. leftMax
stores the maximum value to the left of each index i, and rightMin
stores the minimum value to the right of each index i. We then iterate through the input array nums
from index 1 to index n - 2 (inclusive) and check if the current value is greater than the maximum to its left and less than the minimum to its right. If both conditions are fulfilled, we increase the result by 1. Finally, we return the result as the sum of the beauty of all nums[i]
where 1 <= i <= nums.length - 2.