Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class MyCode {
- /**
- * 3 RGB Set
- *
- * Given a string of characters where each character is either 'r', 'g', or 'b',
- * determine the number of complete sets of 'rgb' that can be made with the
- * characters.
- *
- * Parameters
- * Input: str {String}
- * Output: {Integer}
- *
- * Constraints
- * Time: O(N)
- * Space: O(1)
- *
- * Examples
- * `'rgbrgb' --> 2`
- * `'rbgrbrgrgbgrrggbbbbrgrgrgrg' --> 7`
- * `'bbrr' --> 0`
- */
- public static void main (String[] args) {
- String one = "rgbrgb" ;
- String three = "rbgrbrgrgbgrrggbbbbrgrgrgrg";
- String two = "bbrr";
- System.out.println(rgb(one));
- }
- public static int rgb(String string) {
- HashMap<Character,Integer> map = new HashMap<Character,Integer>(3);
- map.put('r',0);
- map.put('g',0);
- map.put('b',0);
- int count = 0;
- for(int i =0; i< string.length(); i++){
- count = map.get(string.charAt(i));
- map.put(string.charAt(i), ++count);
- }
- int min = map.get('r');
- if(map.get('g') < min){
- min = map.get('g');
- }else if(map.get('b') < min){
- min = map.get('b');
- }
- return min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement