Walmart | Sr. Software Engineer | Mar 2022

Question : You're creating a game with some amusing mini-games, and you've decided to make a simple variant of the game Mahjong.

In this variant, players have a number of tiles, each marked 0-9. The tiles can be grouped into pairs or triples of the same tile. For example, if a player has "333 444 66", the player's hand has a triple of 3s, a triple of 4s, and a pair of 6s. Similarly, "555 55 777" has a triple of 5s, a pair of 5s, and a triple of 7s.

A "complete hand" is defined as a collection of tiles where all the tiles can be grouped into any number of triples (zero or more) and exactly one pair, and each tile is used in exactly one triple or pair.

Write a function that takes a string representation of a collection of tiles in no particular order, and returns true or false depending on whether or not the collection represents a complete hand.

tiles1 = "11133555" # True. 111 33 555
tiles2 = "111333555" # False. There are three triples, 111 333 555 but no pair.
tiles3 = "00000111" # True. 000 00 111. Your pair and a triplet can be of the same value
# There is also no limit to how many of each tile there is.
tiles4 = "13233121" # True. Tiles are not guaranteed to be in order
tiles5 = "11223344555" # False. There cannot be more than one pair
tiles6 = "99999999" # True. You can have many of one tile
tiles7 = "999999999" # False.
tiles8 = "9" # False.
tiles9 = "99" # True. One pair.
tiles10 = "000022" # False.
tiles11 = "888889" # False. There cannot be any tiles left over.
tiles12 = "889" # False. There cannot be any tiles left over.
tiles13 = "88888844" # True. Two triples and one pair
tiles14 = "77777777777777" # True. Four triples and one pair
tiles15 = "1111111" # False.
tiles16 = "1111122222" # False.

Complexity Variable
N - Number of tiles in the input string

Solution :
'''public class GameWithMandatoryOnePairOfTiles_Karat_Interview {
public static void main(String[] argv) {
//All test cases
String tiles1 = "11133555";
String tiles2 = "111333555";
String tiles3 = "00000111";
String tiles4 = "13233121";
String tiles5 = "11223344555";
String tiles6 = "99999999";
String tiles7 = "999999999";
String tiles8 = "9";
String tiles9 = "99";
String tiles10 = "000022";
String tiles11 = "888889";
String tiles12 = "889";
String tiles13 = "88888844";
String tiles14 = "77777777777777";
String tiles15 = "1111111";
String tiles16 = "1111122222";
String tiles17 = "2244";
String tiles18 = "22222";

boolean isSuccess = validGame(tiles12);
System.out.println("Are game rules satisfied: "+ isSuccess);

}

private static boolean validGame(String inputString) {

  boolean isSuccess=false;

  char[] charArray = inputString.toCharArray();

  Arrays.sort(charArray);
 
  Map<Integer, StringBuilder> hashMap = new HashMap<Integer, StringBuilder>();
  StringBuilder innerSb	= new StringBuilder();
		  
  for(int i=0; i< charArray.length; i++) {

    int key = charArray[i]-'0';
    
    if(hashMap.get(key) == null)
        hashMap.put(key, new StringBuilder().append(charArray[i]));
    else{
      hashMap.get(key).append(charArray[i]);
    }
  }
  
for(Map.Entry<Integer, StringBuilder> entry: hashMap.entrySet()) {
      
   StringBuilder mapValue = entry.getValue();
   int countValue = mapValue.length();

   System.out.println("countValue initially == " + countValue) ;   

    if(countValue < 2) 
      return false;
    
    if(hashMap.size() == 1 && (countValue == 2 || countValue % 3 == 2)) {
        return true;
    }

    if(countValue == 2 || (countValue > 2 && hashMap.size() !=1 && countValue % 3 == 2)) {
    	if(isSuccess)		// a pair already found
    		return false;
    	
    	isSuccess=true;
    	continue;
    }
    
    if(countValue > 2) {
        
    	if(countValue % 3 == 0)
    		continue;
    	else
    		return false;
    }
}
	
return isSuccess;

}

}

Comments (1)