There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.

You are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where:

  • The first character of the ith pair denotes the ith ring's color ('R', 'G', 'B').
  • The second character of the ith pair denotes the rod that the ith ring is placed on ('0' to '9').

For example, "R3G2B1 " describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.

Return the number of rods that have all three colors of rings on them.

Example 1:

Input: rings = "B0B6G0R6R0R6G9 " Output: 1 Explanation: - The rod labeled 0 holds 3 rings with all colors: red, green, and blue. - The rod labeled 6 holds 3 rings, but it only has red and blue. - The rod labeled 9 holds only a green ring. Thus, the number of rods with all three colors is 1.

Example 2:

Input: rings = "B0R0G0R9R0B0G0 " Output: 1 Explanation: - The rod labeled 0 holds 6 rings with all colors: red, green, and blue. - The rod labeled 9 holds only a red ring. Thus, the number of rods with all three colors is 1.

Example 3:

Input: rings = "G4 " Output: 0 Explanation: Only one ring is given. Thus, no rods have all three colors.

Constraints:

  • rings.length == 2 * n
  • 1 <= n <= 100
  • rings[i] where i is even is either 'R', 'G', or 'B' (0-indexed).
  • rings[i] where i is odd is a digit from '0' to '9' (0-indexed).

Note: This problem is from LeetCode.
βœ… Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer :: i, j, k, n, m, num_roads
    character(len=2) :: rings
    character(len=1), dimension(:), allocatable :: colors
    integer, dimension(:), allocatable :: positions

    ! read input
    read(*,*) n
    allocate(colors(n))
    allocate(positions(n))
    read(*,*) rings

    ! parse input
    do i = 1, n
        j = (i - 1)*2 + 1
        k = (i - 1)*2 + 2
        colors(i) = rings(j:j)
        positions(i) = iachar(rings(k:k)) - iachar('0')
    end do

    ! solve problem
    num_roads = 0
    do i = 0, 9
        if (count(positions == i) == 3) then
            num_roads = num_roads + 1
        end if
    end do

    ! print output
    write(*,*) num_roads

end program main
❌ Compiled
❌ Executed
❌ Correct
module rings

implicit none

contains

integer function count_rods(rings) result(num_rods)

! This function takes a string of length 2n that describes the n rings
! placed onto the rods. Every two characters in rings forms a color-position
! pair that is used to describe each ring where:
!
! * The first character of the ith pair denotes the ith ring's color
!   ('R', 'G', 'B').
! * The second character of the ith pair denotes the rod that the ith ring
!   is placed on (0 to 9).
!
! For example, "R3G2B1 " describes n == 3 rings: a red ring placed onto the
! rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring
! placed onto the rod labeled 1.
!
! Return the number of rods that have all three colors of rings on them.

integer :: i, j, k, n
character(len=2) :: color
character(len=10) :: rod
logical :: has_red, has_green, has_blue

! Initialize variables
n = len(rings) / 2
has_red = .false.
has_green = .false.
has_blue = .false.

! Loop through each ring and update the has_color variables
do i = 1, n
    color = rings(2 * (i - 1) + 1:2 * (i - 1) + 2)
    select case (color)
        case ("R")
            has_red = .true.
        case ("G")
            has_green = .true.
        case ("B")
            has_blue = .true.
        case default
            write (*,*) "Invalid color: ", color
            stop
    end select
end do

! Loop through each rod and count the number of rings with all three colors
num_rods = 0
do i = 0, 9
    rod = char(i + 48)
    if (has_red .and. has_green .and. has_blue) then
        num_rods = num_rods + 1
    end if
end do

end function count_rods

end module rings

program test_rings

use rings

implicit none

! Test case 1:
! "B0B6G0R6R0R6G9 "
! Expected output: 1
call test_case("B0B6G0R6R0R6G9 ", 1)

! Test case 2:
! "B0R0G0R9R0B0G0 "
! Expected output: 1
call test_case("B0R0G0R9R0B0G0 ", 1)

! Test case 3:
! "G4 "
! Expected output: 0
call test_case("G4 ", 0)

contains

subroutine test_case(rings, expected)

character(len=*), intent(in) :: rings
integer, intent(in) :: expected

integer :: actual

actual = count_rods(rings)

if (actual == expected) then
    write (*,*) "Pass: ", rings
else
    write (*,*) "Fail: ", rings, " Expected: ", expected, " Actual: ", actual
end if

end subroutine test_case

end program test_rings
🌐 Data from online sources
def findFarmland(land):
    m, n = len(land), len(land[0])
    res = []

    for i in range(m):
        for j in range(n):
            if land[i][j] == 1:
                r, c = i, j
                while r < m and land[r][j] == 1: r += 1
                while c < n and land[i][c] == 1: c += 1
                for x in range(i, r):
                    for y in range(j,c):
                        land[x][y] = 0
                res.append([i, j, r - 1, c - 1])

    return res
1. Iterate over each cell (i, j) in the given land matrix.
  1. If the cell value is 1 (farmland), initiate two pointers (r and c) at the current cell.
  2. Move r downwards while the cells in the same column have value 1.
  3. Move c rightwards while the cells in the same row have value 1.
  4. Set all cells within the rectangle (i, j, r-1, c-1) to 0, and add [i, j, r-1, c-1] to the result list.
  5. Repeat steps 1-5 for each cell in the land matrix.
  6. Return the result list with coordinates for each group of farmland.
🌐 Data from online sources
#include <vector>
using namespace std;

vector<vector<int>> findFarmland(vector<vector<int>>& land) {
    int m = land.size(), n = land[0].size();
    vector<vector<int>> res;

    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (land[i][j] == 1) {
                int r = i, c = j;
                while (r < m && land[r][j] == 1) ++r;
                while (c < n && land[i][c] == 1) ++c;
                for (int x = i; x < r; ++x) {
                    for (int y = j; y < c; ++y) {
                        land[x][y] = 0;
                    }
                }
                res.push_back({i, j, r - 1, c - 1});
            }
        }
    }

    return res;
}
1. Iterate over each cell (i, j) in the given land matrix.
  1. If the cell value is 1 (farmland), initiate two pointers (r and c) at the current cell.
  2. Move r downwards while the cells in the same column have value 1.
  3. Move c rightwards while the cells in the same row have value 1.
  4. Set all cells within the rectangle (i, j, r-1, c-1) to 0, and add [i, j, r-1, c-1] to the result list.
  5. Repeat steps 1-5 for each cell in the land matrix.
  6. Return the result list with coordinates for each group of farmland.