How to resolve the algorithm Jewels and stones step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Jewels and stones step by step in the Go programming language

Table of Contents

Problem Statement

Create a function which takes two string parameters: 'stones' and 'jewels' and returns an integer. Both strings can contain any number of upper or lower case letters. However, in the case of 'jewels', all letters must be distinct. The function should count (and return) how many 'stones' are 'jewels' or, in other words, how many letters in 'stones' are also letters in 'jewels'.

Note that: So, for example, if passed "aAAbbbb" for 'stones' and "aA" for 'jewels', the function should return 3. This task was inspired by this problem.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jewels and stones step by step in the Go programming language

The provided code defines a function js that takes two strings as input: stones and jewels. The function returns an integer representing the number of unique characters in stones that are also present in jewels.

There are four different implementations of the js function in the code. Each implementation uses a different approach to solve the problem:

1. Byte Loop and String Index Search: In this implementation, the function iterates over each byte in the stones string using a range loop. For each byte, it checks if the byte is present in the jewels string by using the strings.IndexByte function. If the byte is found, the n counter is incremented.

2. Byte Loop and String Count: This implementation is similar to the previous one, but instead of using the strings.IndexByte function, it uses the strings.Count function to count the number of occurrences of each byte in the stones string that matches a byte in the jewels string.

3. Byte Loop and Set Lookup: This implementation creates a set of bytes from the jewels string. Then, it iterates over each byte in the stones string and checks if the byte is present in the set. If the byte is found, the n counter is incremented.

4. Byte Loop and Counter Map: This implementation creates a map of bytes from the stones string, where the keys are the bytes and the values are the number of occurrences of each byte. Then, it iterates over each byte in the jewels string and checks if the byte is present in the map. If the byte is found, the n counter is incremented by the number of occurrences of that byte in the stones string.

The provided code also includes a main function that demonstrates the usage of the js function. In the main function, the js function is called with two input strings: "aAAbbbb" and "aA". The result, which is the number of unique characters in "aAAbbbb" that are also present in "aA", is printed to the console.

Source code in the go programming language

package main
  
import (
    "fmt"
    "strings"
)

func js(stones, jewels string) (n int) {
    for _, b := range []byte(stones) {
        if strings.IndexByte(jewels, b) >= 0 {
            n++
        }
    }
    return
}

func main() {
    fmt.Println(js("aAAbbbb", "aA"))
}


func js(stones, jewels string) (n int) {
    for _, b := range []byte(jewels) {
        n += strings.Count(stones, string(b))
    }
    return
}


func js(stones, jewels string) (n int) {
    var jSet ['z' + 1]int
    for _, b := range []byte(jewels) {
        jSet[b] = 1
    }
    for _, b := range []byte(stones) {
        n += jSet[b]
    }
    return
}


func js(stones, jewels string) (n int) {
    var sset ['z' + 1]int
    for _, b := range []byte(stones) {
        sset[b]++
    }
    for _, b := range []byte(jewels) {
        n += sset[b]
    }
    return
}


  

You may also check:How to resolve the algorithm Knight's tour step by step in the BASIC programming language
You may also check:How to resolve the algorithm Closest-pair problem step by step in the Java programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the Quackery programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the REXX programming language