Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class RedditScrabble {
  6.     public RedditScrabble(String word, String letters) {
  7.         this.word = word;
  8.         this.letters = letters;
  9.     }
  10.    
  11.     private Map<Integer, Integer> occurences = new HashMap<>();
  12.     private final String word;
  13.     private final String letters;
  14.    
  15.     public static void main(String... args) {
  16.         try(Scanner kb = new Scanner(System.in)) {
  17.             // Yeah, if they're not being dicks with the input we won't need this.
  18.             // But why take the chance?
  19.             String word = kb.nextLine().trim().toUpperCase();
  20.             String letters = kb.nextLine().trim().toUpperCase();
  21.            
  22.             // It's not proper java unless we create a new object.
  23.             // And java.lang.String doesn't count!
  24.             RedditScrabble rs = new RedditScrabble(word, letters);
  25.            
  26.             if(rs.canMakeWord()) {
  27.                 System.out.println("We can make the word");
  28.             }
  29.             else {
  30.                 System.out.println("We can not make the word");
  31.             }
  32.         }
  33.        
  34.                
  35.     }
  36.  
  37.     public boolean canMakeWord() {
  38.         setupIncidenceCounter();
  39.         int errors = word.chars().map(this::consumeChar).reduce(Integer::sum).orElse(0);
  40.        
  41.         // In a more enterprisey application we would have logged the remainders in the incidence counters for that sweet Business Intelligence OLAP cube running on Hadoop.
  42.         // But this is /g/ so we just return.
  43.         return 0 == errors;
  44.     }
  45.    
  46.     private void setupIncidenceCounter() {
  47.         // This is slower than using a loop, but it reads much better.
  48.         letters.chars().forEach(this::countChar);
  49.     }
  50.  
  51.     public int consumeChar(int c) {
  52.         int questionMark = Character.getNumericValue('?');
  53.         int counter = occurences.getOrDefault(c, 0);
  54.         if(c == questionMark && counter <= 0) {
  55.             return 1;
  56.         }
  57.         else if(counter <= 0) {
  58.             return consumeChar(questionMark);
  59.         }
  60.         else {
  61.             // of course some retard is going to change --var to var-- and get burned.
  62.             occurences.put(c, --counter);
  63.             return 0;
  64.         }
  65.     }
  66.    
  67.     public void countChar(int c) {
  68.         if(!occurences.containsKey(c)) {
  69.             occurences.put(c, 1);
  70.         }
  71.         else {
  72.             occurences.put(c, occurences.get(c) + 1);
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement