The value of an alphanumeric string can be defined as:
10, if it comprises of digits only.Given an array strs of alphanumeric strings, return the maximum value of any string in strs.
Example 1:
Input: strs = [ "alic3 ", "bob ", "3 ", "4 ", "00000 "] Output: 5 Explanation: - "alic3 " consists of both letters and digits, so its value is its length, i.e. 5. - "bob " consists only of letters, so its value is also its length, i.e. 3. - "3 " consists only of digits, so its value is its numeric equivalent, i.e. 3. - "4 " also consists only of digits, so its value is 4. - "00000 " consists only of digits, so its value is 0. Hence, the maximum value is 5, of "alic3 ".
Example 2:
Input: strs = [ "1 ", "01 ", "001 ", "0001 "] Output: 1 Explanation: Each string in the array has value 1. Hence, we return 1.
Constraints:
1 <= strs.length <= 1001 <= strs[i].length <= 9strs[i] consists of only lowercase English letters and digits.program main
implicit none
integer :: i, j, k, n, m
character(len=:), allocatable :: strs(:)
integer :: max_val
! Example 1
n = 5
allocate(strs(n))
strs(1) = 'alic3 '
strs(2) = 'bob '
strs(3) = '3 '
strs(4) = '4 '
strs(5) = '00000 '
write(*,*) 'Example 1:'
do i = 1, n
write(*,'(A)') trim(strs(i)) // ': ' // str(i)
end do
max_val = max_value(strs)
write(*,*) 'Maximum value: ', max_val
! Example 2
n = 4
allocate(strs(n))
strs(1) = '1 '
strs(2) = '01 '
strs(3) = '001 '
strs(4) = '0001 '
write(*,*) 'Example 2:'
do i = 1, n
write(*,'(A)') trim(strs(i)) // ': ' // str(i)
end do
max_val = max_value(strs)
write(*,*) 'Maximum value: ', max_val
contains
function str(i) result(res)
implicit none
integer, intent(in) :: i
character(len=:), allocatable :: res
if (is_digits(strs(i))) then
res = num_to_str(str_to_num(strs(i)), 10)
else
res = strs(i)
end if
end function str
pure function is_digits(str) result(res)
implicit none
character(len=*), intent(in) :: str
logical :: res
res = .true.
do i = 1, len(str)
if (.not. is_digit(str(i:i))) then
res = .false.
exit
end if
end do
end function is_digits
pure function is_digit(c) result(res)
implicit none
character, intent(in) :: c
logical :: res
res = c >= '0' .and. c <= '9'
end function is_digit
pure function str_to_num(str) result(res)
implicit none
character(len=*), intent(in) :: str
integer :: res
read(str, *) res
end function str_to_num
pure function num_to_str(num, base) result(res)
implicit none
integer, intent(in) :: num, base
character(len=:), allocatable :: res
character(len=:), allocatable :: tmp
integer :: i
if (base == 10) then
res = num_to_str_10(num)
else
allocate(character(len=log10(num)+1) :: tmp)
write(tmp, '(I0)') num
res = tmp
end if
end function num_to_str
pure function num_to_str_10(num) result(res)
implicit none
integer, intent(in) :: num
character(len=:), allocatable :: res
character(len=:), allocatable :: tmp
integer :: i
allocate(character(len=log10(num)+1) :: tmp)
write(tmp, '(I0)') num
res = tmp
end function num_to_str_10
pure function max_value(strs) result(res)
implicit none
character(len=*), intent(in) :: strs(:)
integer :: res
res = -huge(res)
do i = 1, size(strs)
if (str(i) > res) then
res = str(i)
end if
end do
end function max_
temp.f95:10:13:
10 | allocate(strs(n))
| 1
Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag
temp.f95:25:13:
25 | allocate(strs(n))
| 1
Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag
temp.f95:113:21:
113 | end function max_
| 1
Error: Expected label ‘max_value’ for END FUNCTION statement at (1)
f951: Error: Unexpected end of file in ‘temp.f95’
module max_value_of_alphanumeric_strings
implicit none
contains
function max_value(strs) result(max_val)
! Given an array of alphanumeric strings, return the maximum value of any string in the array.
! The value of a string can be defined as:
! - The numeric representation of the string in base 10, if it comprises of digits only.
! - The length of the string, otherwise.
character(len=*), intent(in) :: strs(:)
integer :: max_val
integer :: i, j, num, len
! Initialize the maximum value to the first string in the array.
max_val = len(strs(1))
! Iterate over each string in the array.
do i = 1, size(strs)
! Convert the string to a number if possible.
read(strs(i), *, iostat=j) num
! If the string is a number, calculate its value.
if (j == 0) then
len = len(strs(i))
max_val = max(max_val, num * (len - 1))
! Otherwise, calculate its value as its length.
else
max_val = max(max_val, len(strs(i)))
end if
end do
end function max_value
end module max_value_of_alphanumeric_strings
program test_max_value
use max_value_of_alphanumeric_strings
implicit none
! Test case 1:
print *, max_value(["alic3 ", "bob ", "3 ", "4 ", "00000 "])
! Test case 2:
print *, max_value(["1 ", "01 ", "001 ", "0001 "])
end program test_max_value
temp.f95:29:12:
29 | len = len(strs(i))
| 1
Error: ‘len’ at (1) is not a variable
temp.f95:30:43:
30 | max_val = max(max_val, num * (len - 1))
| 1
Error: Function ‘len’ requires an argument list at (1)
temp.f95:44:5:
44 | use max_value_of_alphanumeric_strings
| 1
Fatal Error: Cannot open module file ‘max_value_of_alphanumeric_strings.mod’ for reading at (1): No such file or directory
compilation terminated.
def maxValue(strs) -> int:
max_value = 0
for str in strs:
if str.isdigit():
value = int(str)
else:
value = len(str)
max_value = max(max_value, value)
return max_value
We iterate through the input array strs. For each string, we check whether it contains only digits or not. If it contains only digits, we find its numeric value and store it in a variable value. If it contains other characters (letters), we store its length in the value variable. In each iteration, we update the max_value variable with the maximum of the current max_value and the value found.
Finally, we return the max_value.
#include <string>
#include <vector>
int maxValue(std::vector<std::string>& strs) {
int max_value = 0;
for (const std::string& str : strs) {
int value;
if (str.find_first_not_of("0123456789") == std::string::npos) {
value = std::stoi(str);
} else {
value = static_cast<int>(str.length());
}
max_value = std::max(max_value, value);
}
return max_value;
}
We iterate through the input array strs. For each string, we check whether it contains only digits or not. If it contains only digits, we find its numeric value and store it in a variable value. If it contains other characters (letters), we store its length in the value variable. In each iteration, we update the max_value variable with the maximum of the current max_value and the value found.
Finally, we return the max_value.