Determine if String Halves Are Alike

🏠 ⬅️ ➡️

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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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
  1. First, initialize variables 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.
  2. For each cell of the matrix, add 1 to the corresponding row and column vectors' elements if the cell value is 1. Thus, by the end of the nested loop, we'll have the count of one's in each row and column in our row and col vectors.
  3. Now, iterate over each cell of the matrix once again. If the cell value is 1, and the corresponding row and column count in row and col vectors are both 1, increment the special_positions count.
  4. Finally, return the special_positions count, which represents the number of special positions in the matrix mat.
🌐 Data from online sources
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;
}
  1. First, initialize variables 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.
  2. For each cell of the matrix, add 1 to the corresponding row and column vectors' elements if the cell value is 1. Thus, by the end of the nested loop, we'll have the count of one's in each row and column in our row and col vectors.
  3. Now, iterate over each cell of the matrix once again. If the cell value is 1, and the corresponding row and column count in row and col vectors are both 1, increment the special_positions count.
  4. Finally, return the special_positions count, which represents the number of special positions in the matrix mat.