You are given a string s
. Reorder the string using the following algorithm:
s
and append it to the result.s
which is greater than the last appended character to the result and append it.s
and append it to the result.s
which is smaller than the last appended character to the result and append it.s
.In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.
Return the result string after sorting s
with this algorithm.
Example 1:
Input: s = "aaaabbbbcccc " Output: "abccbaabccba " Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc " After steps 4, 5 and 6 of the first iteration, result = "abccba " First iteration is done. Now s = "aabbcc " and we go back to step 1 After steps 1, 2 and 3 of the second iteration, result = "abccbaabc " After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba "
Example 2:
Input: s = "rat " Output: "art " Explanation: The word "rat " becomes "art " after re-ordering it with the mentioned algorithm.
Constraints:
1 <= s.length <= 500
s
consists of only lowercase English letters.program main
implicit none
character(len=:), allocatable :: s
character(len=:), allocatable :: result
! Example 1
s = 'aaaabbbbcccc'
write(*,'(A)') solve(s)
! Example 2
s = 'rat'
write(*,'(A)') solve(s)
contains
function solve(s) result(result)
implicit none
character(len=*), intent(in) :: s
integer :: i, j, k
character :: c
logical :: found
allocate(character(len=len(s)) :: result)
result = ''
do while (len(s) > 0)
! Step 1
c = minval(s)
call remove(s, c)
result = trim(result) // c
! Step 2
do while (len(s) > 0)
c = minval(s, key=gt_char)
if (c == '') exit
call remove(s, c)
result = trim(result) // c
end do
! Step 3
do while (len(s) > 0)
c = minval(s)
if (c == '') exit
call remove(s, c)
result = trim(result) // c
end do
! Step 4
c = maxval(s)
call remove(s, c)
result = trim(result) // c
! Step 5
do while (len(s) > 0)
c = maxval(s, key=lt_char)
if (c == '') exit
call remove(s, c)
result = trim(result) // c
end do
! Step 6
do while (len(s) > 0)
c = maxval(s)
if (c == '') exit
call remove(s, c)
result = trim(result) // c
end do
end do
contains
pure function gt_char(c1, c2) result(gt)
implicit none
character, intent(in) :: c1, c2
logical :: gt
gt = ichar(c1) > ichar(c2)
end function gt_char
pure function lt_char(c1, c2) result(lt)
implicit none
character, intent(in) :: c1, c2
logical :: lt
lt = ichar(c1) < ichar(c2)
end function lt_char
subroutine remove(string, char)
implicit none
character(len=*), intent(inout) :: string
character, intent(in) :: char
integer :: i
do while (index(string, char) /= 0)
i = index(string, char)
string(i:) = string(i+1:)
end do
end subroutine remove
end function solve
end program main
temp.f95:24:42: 24 | allocate(character(len=len(s)) :: result) | 1 Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable temp.f95:17:36: 17 | function solve(s) result(result) | 1 Error: Function result ‘result’ at (1) has no IMPLICIT type temp.f95:71:12: 71 | contains | 1 Error: CONTAINS statement at (1) is already in a contained program unit temp.f95:74:25: 74 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:75:43: 75 | character, intent(in) :: c1, c2 | 1 Error: Unexpected data declaration statement at (1) temp.f95:76:25: 76 | logical :: gt | 1 Error: Unexpected data declaration statement at (1) temp.f95:79:28: 79 | end function gt_char | 1 Error: Expected label ‘solve’ for END FUNCTION statement at (1) temp.f95:82:25: 82 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:83:43: 83 | character, intent(in) :: c1, c2 | 1 Error: Unexpected data declaration statement at (1) temp.f95:84:25: 84 | logical :: lt | 1 Error: Unexpected data declaration statement at (1) temp.f95:87:28: 87 | end function lt_char | 1 Error: Expected label ‘solve’ for END FUNCTION statement at (1) temp.f95:90:25: 90 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:91:53: 91 | character(len=*), intent(inout) :: string | 1 Error: Unexpected data declaration statement at (1) temp.f95:92:41: 92 | character, intent(in) :: char | 1 Error: Unexpected data declaration statement at (1) temp.f95:93:24: 93 | integer :: i | 1 Error: Symbol ‘i’ at (1) already has basic type of INTEGER temp.f95:99:11: 99 | end subroutine remove | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:24:31: 24 | allocate(character(len=len(s)) :: result) | 1 Error: ‘remove’ at (1) is not a function temp.f95:78:25: 78 | gt = ichar(c1) > ichar(c2) | 1 Error: Symbol ‘c1’ at (1) has no IMPLICIT type; did you mean ‘c’? temp.f95:78:37: 78 | gt = ichar(c1) > ichar(c2) | 1 Error: Symbol ‘c2’ at (1) has no IMPLICIT type; did you mean ‘c’? temp.f95:95:40: 95 | do while (index(string, char) /= 0) | 1 Error: Symbol ‘char’ at (1) has no IMPLICIT type temp.f95:78:14: 78 | gt = ichar(c1) > ichar(c2) | 1 Error: Symbol ‘gt’ at (1) has no IMPLICIT type temp.f95:35:41: 35 | c = minval(s, key=gt_char) | 1 Error: Symbol ‘gt_char’ at (1) has no IMPLICIT type temp.f95:86:14: 86 | lt = ichar(c1) < ichar(c2) | 1 Error: Symbol ‘lt’ at (1) has no IMPLICIT type temp.f95:56:41: 56 | c = maxval(s, key=lt_char) | 1 Error: Symbol ‘lt_char’ at (1) has no IMPLICIT type temp.f95:95:34: 95 | do while (index(string, char) /= 0) | 1 Error: Symbol ‘string’ at (1) has no IMPLICIT type temp.f95:29:23: 29 | c = minval(s) | 1 Error: ‘array’ argument of ‘minval’ intrinsic at (1) must be an array temp.f95:43:27: 43 | c = minval(s) | 1 Error: ‘array’ argument of ‘minval’ intrinsic at (1) must be an array temp.f95:50:23: 50 | c = maxval(s) | 1 Error: ‘array’ argument of ‘maxval’ intrinsic at (1) must be an array temp.f95:64:27: 64 | c = maxval(s) | 1 Error: ‘array’ argument of ‘maxval’ intrinsic at (1) must be an array
! Declare variables
integer :: i, j, k, l, m, n, o
character(len=500) :: s, result
! Read input
read(*,*) s
! Initialize variables
i = 1
j = 1
k = 1
l = 1
m = 1
n = 1
o = 1
! Initialize result
result = ""
! Loop until all characters are used
do while (i <= len(s))
! Find smallest character
do while (j <= len(s))
if (s(j:j) < s(i:i)) then
i = j
endif
j = j + 1
enddo
! Append smallest character
result = result // s(i:i)
! Find smallest character greater than last appended character
do while (k <= len(s))
if (s(k:k) > s(i:i) .and. s(k:k) < s(o:o)) then
o = k
endif
k = k + 1
enddo
! Append smallest character greater than last appended character
result = result // s(o:o)
! Find largest character
do while (l <= len(s))
if (s(l:l) > s(i:i)) then
i = l
endif
l = l + 1
enddo
! Append largest character
result = result // s(i:i)
! Find largest character smaller than last appended character
do while (m <= len(s))
if (s(m:m) < s(i:i) .and. s(m:m) > s(n:n)) then
n = m
endif
m = m + 1
enddo
! Append largest character smaller than last appended character
result = result // s(n:n)
! Increment variables
i = i + 1
j = j + 1
k = k + 1
l = l + 1
m = m + 1
n = n + 1
o = o + 1
! End loop
enddo
! Print result
print *, result
end
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x79062fb54960 in ??? #1 0x79062fb554d9 in ??? #2 0x79062fda917b in ??? #3 0x79062fda2684 in ??? #4 0x79062fda32aa in ??? #5 0x5d09c18a721c in MAIN__ #6 0x5d09c18a7655 in main
def numberOfSubarrays(nums, k):
n = len(nums)
result = count = 0
odd_count = [0] * (n + 1)
odd_count[0] = 1
for num in nums:
count += num % 2
if count >= k:
result += odd_count[count - k]
odd_count[count] += 1
return result
oddCount
with a size of n+1
and set oddCount[0]
to 1.count
and result
to 0.nums
.count
by the value of the element modulo 2 (if the element is odd, it increments by 1, otherwise by 0).count
is greater than or equal to k
, increment result
by the value of oddCount[count - k]
.oddCount[count]
.result
as the number of nice sub-arrays.This algorithm works by tracking the number of odd elements within a subarray and storing the cumulative sums in the oddCount
array. By iterating through the input array and incrementing the count, the algorithm determines whether a subarray is nice by checking if the count is greater than or equal to k
and updating the result accordingly.
int numberOfSubarrays(vector<int>& nums, int k) {
int n = nums.size(), result = 0, count = 0;
vector<int> oddCount(n + 1, 0);
oddCount[0] = 1;
for (int i = 0; i < n; i++) {
count += nums[i] % 2;
if (count >= k) result += oddCount[count - k];
oddCount[count]++;
}
return result;
}
oddCount
with a size of n+1
and set oddCount[0]
to 1.count
and result
to 0.nums
.count
by the value of the element modulo 2 (if the element is odd, it increments by 1, otherwise by 0).count
is greater than or equal to k
, increment result
by the value of oddCount[count - k]
.oddCount[count]
.result
as the number of nice sub-arrays.This algorithm works by tracking the number of odd elements within a subarray and storing the cumulative sums in the oddCount
array. By iterating through the input array and incrementing the count, the algorithm determines whether a subarray is nice by checking if the count is greater than or equal to k
and updating the result accordingly.