You are given a string word
that consists of digits and lowercase English letters.
You will replace every non-digit character with a space. For example, "a123bc34d8ef34 "
will become " 123 34 8 34 "
. Notice that you are left with some integers that are separated by at least one space: "123 "
, "34 "
, "8 "
, and "34 "
.
Return the number of different integers after performing the replacement operations on word
.
Two integers are considered different if their decimal representations without any leading zeros are different.
Example 1:
Input: word = "a123bc34d8ef34 " Output: 3 Explanation: The three different integers are "123 ", "34 ", and "8 ". Notice that "34 " is only counted once.
Example 2:
Input: word = "leet1234code234 " Output: 2
Example 3:
Input: word = "a1b01c001 " Output: 1 Explanation: The three integers "1 ", "01 ", and "001 " all represent the same integer because the leading zeros are ignored when comparing their decimal values.
Constraints:
1 <= word.length <= 1000
word
consists of digits and lowercase English letters.program main
implicit none
character(len=1000) :: word
integer :: n
call get_command_argument(1, word)
n = count_integers(word)
print '(I0)', n
contains
function count_integers(str) result(n)
implicit none
character(*), intent(in) :: str
integer :: i, j, k, n
n = 0
do i = 1, len(str)
if (is_integer(str(i:))) then
n = n + 1
end if
end do
end function count_integers
pure logical function is_integer(str)
implicit none
character(*), intent(in) :: str
integer :: i, j
is_integer = .false.
do i = 1, len(str)
if (.not. is_digit(str(i:))) exit
end do
if (i > 1) then
is_integer = .true.
return
end if
do i = 1, len(str) - 1
if (.not. is_digit(str(i:))) cycle
do j = i + 1, len(str)
if (.not. is_digit(str(j:))) exit
end do
if (j > i + 1) then
is_integer = .true.
return
end if
end do
end function is_integer
pure logical function is_digit(str)
implicit none
character(*), intent(in) :: str
integer :: i
is_digit = .false.
do i = 1, len(str)
if (str(i:) == ' ') exit
if (str(i:) >= '0' .and. str(i:) <= '9') then
is_digit = .true.
return
end if
end do
end function is_digit
end program main
0
module Main
implicit none
interface
function count_different_integers(word) result(res)
character(len=*), intent(in) :: word
integer :: res
end function count_different_integers
end interface
contains
subroutine run_test_cases()
integer :: i
character(len=1000) :: word
integer :: res
word = "a123bc34d8ef34 "
res = count_different_integers(word)
if (res .ne. 3) then
write (*,*) "Test case 1 failed. Expected 3, got ", res
end if
word = "leet1234code234 "
res = count_different_integers(word)
if (res .ne. 2) then
write (*,*) "Test case 2 failed. Expected 2, got ", res
end if
word = "a1b01c001 "
res = count_different_integers(word)
if (res .ne. 1) then
write (*,*) "Test case 3 failed. Expected 1, got ", res
end if
end subroutine run_test_cases
end module Main
!---------------------------------------------------------------------------------------------------------------------------------
module IntegerUtils
implicit none
private
public :: count_different_integers
contains
function count_different_integers(word) result(res)
character(len=*), intent(in) :: word
integer :: res
! Replace non-digit characters with spaces
word = trim(adjustl(word))
word = transfer(word, word, 1000)
! Count the number of integers
res = 0
do i = 1, len_trim(word)
if (word(i:i) .eq. ' ') then
res = res + 1
end if
end do
end function count_different_integers
end module IntegerUtils
!---------------------------------------------------------------------------------------------------------------------------------
program main
use IntegerUtils
implicit none
character(len=1000) :: word
integer :: res
! Read the input word from the command line
word = ' '
call get_command_argument(1, word)
! Count the number of different integers
res = count_different_integers(word)
! Print the result
write (*,*) res
end program main
temp.f95:65:4: 65 | do i = 1, len_trim(word) | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:60:0: 60 | word = trim(adjustl(word)) | Error: Dummy argument ‘word’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:61:0: 61 | word = transfer(word, word, 1000) | Error: Dummy argument ‘word’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:77:12: 1 | module Main | 2 ...... 77 | program main | 1 Error: Global name ‘main’ at (1) is already being used as a MODULE at (2) temp.f95:79:5: 79 | use IntegerUtils | 1 Fatal Error: Cannot open module file ‘integerutils.mod’ for reading at (1): No such file or directory compilation terminated.
def min_moves(nums, k):
n = len(nums)
ones = [i - j for i, j in enumerate(nums) if j == 1]
moves = sum(ones[i] - ones[k // 2] for i in range(k))
min_moves = moves
for i in range(k, len(ones)):
moves += ones[i] - ones[i - k] - k
min_moves = min(min_moves, moves)
return min_moves
First, we extract the positions of 1's into a new list and subtract the index of 1's to get the relative positions. Then we calculate the moves required to make the first k 1's consecutive. For each consecutive block of k 1's, we calculate the moves required to make them consecutive and update the minimum moves if it is less than the current minimum moves. To get the moves for the next block of k 1's, we just add the difference in positions and subtract k from the current moves, as one extra move is needed to move past the last 1 in the previous k 1's.
int minMoves(vector<int>& nums, int k) {
int n = nums.size();
vector<int> ones;
for(int i = 0; i < n; ++i){
if(nums[i] == 1){
ones.push_back(i - ones.size());
}
}
int moves = 0;
for(int i = 0; i < k; ++i){
moves += (ones[i] - ones[k / 2]);
}
int min_moves = moves;
for(int i = k; i < ones.size(); ++i){
moves += ones[i] - ones[i - k] - k;
min_moves = min(min_moves, moves);
}
return min_moves;
}
First, we extract the positions of 1's into a new list and subtract the index of 1's to get the relative positions. Then we calculate the moves required to make the first k 1's consecutive. For each consecutive block of k 1's, we calculate the moves required to make them consecutive and update the minimum moves if it is less than the current minimum moves. To get the moves for the next block of k 1's, we just add the difference in positions and subtract k from the current moves, as one extra move is needed to move past the last 1 in the previous k 1's.