[algorithm] 2103 Ring and rod (Java / C / C + + / Python / go / trust)

1281. Difference between bit product sum of integers:

There are n rings in total, and the color of the ring can be one of red, green and blue. These rings are distributed on 10 rods numbered 0 to 9.

Give you a string rings with a length of 2n to represent the distribution of these n rings on the rod. Every two characters in rings form a color position pair to describe each ring:

  • The first character in the i-th pair represents the color of the i-th ring ('R ',' G ',' B ').
  • The second character in the i-th pair indicates the position of the i-th ring, that is, on which rod ('0 'to' 9 ').

For example, "R3G2B1" means that there are n == 3 rings in total, the red ring is on the rod No. 3, the green ring is on the rod No. 2, and the blue ring is on the rod No. 1.

Find all the rods that gather all three color rings and return the number of such rods.

Example 1:


Input:
rings = "B0B6G0R6R0R6G9"

Output:
	1
	
Explanation:
	- There are 3 rings on the rod No. 0, which collect all colors: red, green and blue.
	- The rod No. 6 has three rings, but only red and blue.
	- There is only one green ring on rod No. 9.
	Therefore, the number of rods integrating all three color rings is 1.

Example 2:


Input:
rings = "B0R0G0R9R0B0G0"

Output:
	1
	
Explanation:
	- There are 6 rings on the rod No. 0, which collect all colors: red, green and blue.
	- There is only one red ring on rod No. 9.
	Therefore, the number of rods integrating all three color rings is 1.

Example 3:

Input:
	rings = "G4"
	
Output:
	0
	
Explanation:
	Only one ring is given, so there is no rod that gathers all three color rings.

Tips:

  • rings.length == 2 * n
  • 1 <= n <= 100
  • If I is an even number, the value of rings[i] can take 'R', 'G' or 'B' (subscript counts from 0)
  • If I is an odd number, the value of rings[i] can take a number from '0' to '9' (the subscript counts from 0)

analysis

  • Facing this algorithm problem, the second leader fell into meditation.
  • There are 10 rods numbered from 0 to 9. We need to count how many rods there are, and there are rings of three colors.
  • You can double-layer map. The key of the first layer is the number of the rod, the key of the second layer is the color, and the value of the second layer uses Boolean statistics to determine whether the color exists.
  • Optimization, you can use bit operation. There are only three colors, so you can use three bits to indicate whether the color exists. In this way, only the int array with length of 10 is needed to count the rod state.

Problem solution

java

class Solution {
    public int countPoints(String rings) {
        int ans = 0;

        int   n       = rings.length();
        int[] counter = new int[10];
        for (int i = 0; i < n; i += 2) {
            char color = rings.charAt(i);
            int  index = rings.charAt(i + 1) - '0';
            switch (color) {
                case 'R':
                    counter[index] |= 1;
                    break;
                case 'G':
                    counter[index] |= 2;
                    break;
                default:
                    counter[index] |= 4;
                    break;
            }
        }

        for (int c : counter) {
            if (c == 7) {
                ++ans;
            }
        }

        return ans;
    }
}

c

int countPoints(char * rings){
    int ans = 0;

    int counter[10] = {0};
    while (*rings) {
        char color = *rings;
        ++rings;
        int index = *rings - '0';
        ++rings;
        switch (color) {
            case 'R':
                counter[index] |= 1;
                break;
            case 'G':
                counter[index] |= 2;
                break;
            default:
                counter[index] |= 4;
                break;
        }
    }

    for (int i = 0; i < 10; ++i) {
        if (counter[i] == 7) {
            ++ans;
        }
    }

    return ans;
}

c++

class Solution {
public:
    int countPoints(string rings) {
        int ans = 0;

        int n = rings.size();
        int counter[10] = {0};
        for (int i = 0; i < n; i += 2) {
            char color = rings[i];
            int index = rings[i + 1] - '0';
            switch (color) {
                case 'R':
                    counter[index] |= 1;
                    break;
                case 'G':
                    counter[index] |= 2;
                    break;
                default:
                    counter[index] |= 4;
                    break;
            }
        }

        for (int n: counter) {
            if (n == 7) {
                ++ans;
            }
        }

        return ans;
    }
};

python

class Solution:
    def countPoints(self, rings: str) -> int:
        ans = 0
        counter = [0] * 10
        for i in range(0, len(rings), 2):
            c = rings[i]
            idx = int(rings[i + 1])
            if c == 'R':
                counter[idx] |= 1
            elif c == 'G':
                counter[idx] |= 2
            else:
                counter[idx] |= 4
        for n in counter:
            if n == 7:
                ans += 1
        return ans
        

go

func countPoints(rings string) int {
    ans := 0

	counter := make([]int, 10)
	n := len(rings)
	for i := 0; i < n; i += 2 {
		c := rings[i]
		idx := rings[i+1] - '0'
		switch c {
		case 'R':
			counter[idx] |= 1
		case 'G':
			counter[idx] |= 2
		default:
			counter[idx] |= 4
		}
	}
	for _, n := range counter {
		if n == 7 {
			ans++
		}
	}

	return ans
}

rust

impl Solution {
    pub fn count_points(rings: String) -> i32 {
        let mut counter = vec![0; 10];
        rings.as_bytes().chunks(2).for_each(|c| {
            let color = match c[0] as char {
                'R' => 1,
                'G' => 2,
                _ => 4
            };
            let index = (c[1] - '0' as u8) as usize;
            counter[index] |= color;
        });

        counter.iter().filter(|n| {
            **n == 7
        }).count() as i32
    }
}

Original title portal: https://leetcode-cn.com/problems/rings-and-rods/

Thank you very much for reading this article~
Welcome[ 👍 [like][ ⭐ Collection][ 📝 [comments]~
It's not hard to give up, but it must be cool to insist~
I hope all of us can make a little progress every day~
This paper consists of White hat of the second leader: https://le-yi.blog.csdn.net/ Blog originality~

Keywords: Python Java Go Rust

Added by geo3d on Thu, 13 Jan 2022 05:32:22 +0200