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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jewels and stones step by step in the Objeck 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 Objeck programming language

Source code in the objeck programming language

use Collection.Generic;

class JewelsStones {
  function : Main(args : String[]) ~ Nil {
    Count("aAAbbbb", "aA")->PrintLine();
    Count("ZZ", "z")->PrintLine();
  }

  function : Count(stones : String, jewels : String) ~ Int {
    bag := Set->New();

    each(i : jewels) {
      bag->Insert(jewels->Get(i));
    };

    count := 0;
    each(i : stones) {
      if(bag->Has(stones->Get(i))) {
        count++;
      };
    };
 
    return count;
  }
}

  

You may also check:How to resolve the algorithm Menu step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Program termination step by step in the Forth programming language
You may also check:How to resolve the algorithm 2048 step by step in the Factor programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the Ada programming language
You may also check:How to resolve the algorithm Bin given limits step by step in the EasyLang programming language