You are given a string s
of even length. Split this string into two halves of equal lengths, and let a
be the first half and b
be the second half.
Two strings are alike if they have the same number of vowels ('a'
, 'e'
, 'i'
, 'o'
, 'u'
, 'A'
, 'E'
, 'I'
, 'O'
, 'U'
). Notice that s
contains uppercase and lowercase letters.
Return true
if a
and b
are alike. Otherwise, return false
.
Example 1:
Input: s = "book " Output: true Explanation: a = "bo " and b = "ok ". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
Example 2:
Input: s = "textbook " Output: false Explanation: a = "text " and b = "book ". a has 1 vowel whereas b has 2. Therefore, they are not alike. Notice that the vowel o is counted twice.
Constraints:
2 <= s.length <= 1000
s.length
is even.s
consists of uppercase and lowercase letters.program main
implicit none
character(len=:), allocatable :: s
logical :: result
! Example 1
s = "book "
write(*,*) "Example 1:"
write(*,*) "Input: ", s
result = alike(s)
write(*,*) "Output: ", result
write(*,*) ""
! Example 2
s = "textbook "
write(*,*) "Example 2:"
write(*,*) "Input: ", s
result = alike(s)
write(*,*) "Output: ", result
write(*,*) ""
contains
logical function alike(s)
implicit none
character(len=*), intent(in) :: s
integer :: nvowels
integer :: i
nvowels = 0
do i = 1, len(s)/2
select case (s(i:i))
case ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
nvowels = nvowels + 1
end select
end do
alike = (nvowels == nvowels(s(len(s)/2+1:)))
end function alike
integer function nvowels(s)
implicit none
character(len=*), intent(in) :: s
integer :: i
nvowels = 0
do i = 1, len(s)
select case (s(i:i))
case ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
nvowels = nvowels + 1
end select
end do
end function nvowels
end program main
temp.f95:39:32: 39 | alike = (nvowels == nvowels(s(len(s)/2+1:))) | 1 Error: Expected a right parenthesis in expression at (1)
module alike_strings
implicit none
contains
function alike_strings(s) result(alike)
! Given a string s of even length, split it into two halves of equal lengths,
! and let a be the first half and b be the second half.
! Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U').
! Notice that s contains uppercase and lowercase letters.
character(len=*), intent(in) :: s
logical :: alike
integer :: n, i, j, vowels_a, vowels_b
character(len=1) :: c
! Initialize variables
n = len(s) / 2
vowels_a = 0
vowels_b = 0
! Count the number of vowels in each half
do i = 1, n
c = s(i:i)
if (c == 'a' .or. c == 'e' .or. c == 'i' .or. c == 'o' .or. c == 'u' .or. &
c == 'A' .or. c == 'E' .or. c == 'I' .or. c == 'O' .or. c == 'U') then
vowels_a = vowels_a + 1
end if
end do
do j = n + 1, len(s)
c = s(j:j)
if (c == 'a' .or. c == 'e' .or. c == 'i' .or. c == 'o' .or. c == 'u' .or. &
c == 'A' .or. c == 'E' .or. c == 'I' .or. c == 'O' .or. c == 'U') then
vowels_b = vowels_b + 1
end if
end do
! Check if the number of vowels in each half is the same
if (vowels_a == vowels_b) then
alike = .true.
else
alike = .false.
end if
end function alike_strings
end module alike_strings
program test_alike_strings
use alike_strings
implicit none
! Examples
print *, alike_strings("book")
print *, alike_strings("textbook")
print *, alike_strings("aa")
print *, alike_strings("aeiou")
print *, alike_strings("aaeeeioouu")
end program test_alike_strings
temp.f95:7:22: 7 | function alike_strings(s) result(alike) | 1 Error: MODULE attribute of ‘alike_strings’ conflicts with PROCEDURE attribute at (1) temp.f95:14:33: 14 | character(len=*), intent(in) :: s | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:15:16: 15 | logical :: alike | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:38: 17 | integer :: n, i, j, vowels_a, vowels_b | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:18:21: 18 | character(len=1) :: c | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:21:14: 21 | n = len(s) / 2 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:12: 22 | vowels_a = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:12: 23 | vowels_b = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:11: 26 | do i = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:27:12: 27 | c = s(i:i) | 1 Error: Syntax error in argument list at (1) temp.f95:29:78: 29 | c == 'A' .or. c == 'E' .or. c == 'I' .or. c == 'O' .or. c == 'U') then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:30:31: 30 | vowels_a = vowels_a + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:31:7: 31 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:32:3: 32 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:34:20: 34 | do j = n + 1, len(s) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:35:12: 35 | c = s(j:j) | 1 Error: Syntax error in argument list at (1) temp.f95:37:78: 37 | c == 'A' .or. c == 'E' .or. c == 'I' .or. c == 'O' .or. c == 'U') then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:38:31: 38 | vowels_b = vowels_b + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:39:7: 39 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:40:3: 40 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:43:30: 43 | if (vowels_a == vowels_b) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:44:18: 44 | alike = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:45:4: 45 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:46:19: 46 | alike = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:47:3: 47 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:49:3: 49 | end function alike_strings | 1 Error: Expecting END MODULE statement at (1) temp.f95:55:5: 55 | use alike_strings | 1 Fatal Error: Cannot open module file ‘alike_strings.mod’ for reading at (1): No such file or directory compilation terminated.
def numSpecial(mat):
m, n = len(mat), len(mat[0])
row, col = [0] * m, [0] * n
for i in range(m):
for j in range(n):
if mat[i][j] == 1:
row[i] += 1
col[j] += 1
special_positions = 0
for i in range(m):
for j in range(n):
if mat[i][j] == 1 and row[i] == 1 and col[j] == 1:
special_positions += 1
return special_positions
m
and n
to be the number of rows and columns of the given matrix mat
. Also, create two vectors row
and col
of size m
and n
respectively, and initialize them with all zeros. These vectors will be used to store the one's count of each row and column in the matrix.row
and col
vectors.row
and col
vectors are both 1, increment the special_positions count.mat
.int numSpecial(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size();
vector<int> row(m, 0), col(n, 0);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1) {
++row[i];
++col[j];
}
}
}
int special_positions = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1 && row[i] == 1 && col[j] == 1) {
++special_positions;
}
}
}
return special_positions;
}
m
and n
to be the number of rows and columns of the given matrix mat
. Also, create two vectors row
and col
of size m
and n
respectively, and initialize them with all zeros. These vectors will be used to store the one's count of each row and column in the matrix.row
and col
vectors.row
and col
vectors are both 1, increment the special_positions count.mat
.