Advertisement
gelita

3 RGB set

Feb 8th, 2020
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.30 KB | None | 0 0
  1.  
  2.   import java.io.*;
  3.   import java.util.*;
  4.  
  5.   class MyCode {
  6.  /**
  7.    * 3 RGB Set
  8.    *
  9.    * Given a string of characters where each character is either 'r', 'g', or 'b',
  10.    * determine the number of complete sets of 'rgb' that can be made with the
  11.    * characters.
  12.    *
  13.    * Parameters
  14.    * Input: str {String}
  15.    * Output: {Integer}
  16.    *
  17.    * Constraints
  18.    * Time: O(N)
  19.    * Space: O(1)
  20.    *
  21.    * Examples
  22.    * `'rgbrgb' --> 2`
  23.    * `'rbgrbrgrgbgrrggbbbbrgrgrgrg' --> 7`
  24.    * `'bbrr' --> 0`
  25.    */
  26.  
  27.     public static void main (String[] args) {
  28.       String one = "rgbrgb" ;
  29.       String three = "rbgrbrgrgbgrrggbbbbrgrgrgrg";
  30.       String two = "bbrr";
  31.       System.out.println(rgb(one));  
  32.     }
  33.       public static int rgb(String string) {
  34.         HashMap<Character,Integer> map = new  HashMap<Character,Integer>(3);
  35.         map.put('r',0);
  36.         map.put('g',0);
  37.         map.put('b',0);
  38.         int count = 0;
  39.         for(int i =0; i< string.length(); i++){
  40.           count = map.get(string.charAt(i));
  41.           map.put(string.charAt(i), ++count);
  42.         }        
  43.         int min = map.get('r');
  44.         if(map.get('g') < min){
  45.           min = map.get('g');
  46.         }else if(map.get('b') < min){
  47.           min = map.get('b');
  48.         }
  49.         return min;
  50.      }
  51.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement