@Test public void canConstruct() { assertFalse(RansomNoteSolution.canConstruct("a", "b")); assertFalse(RansomNoteSolution.canConstruct("aa", "ab")); assertTrue(RansomNoteSolution.canConstruct("aa", "aba")); assertTrue(RansomNoteSolution.canConstruct("aa", "aab")); }
static boolean canConstruct(String ransomNote, String magazine) { int[] letters = new int[26]; int a = 'a'; for (int i = 0; i < magazine.length(); i++) { letters[magazine.charAt(i) - a]++; } for (int i = 0; i < ransomNote.length(); i++) { if (--letters[ransomNote.charAt(i) - a] < 0) { return false; } } return true; }
RansomNoteSolution { static boolean canConstruct(String ransomNote, String magazine) { int[] letters = new int[26]; int a = 'a'; for (int i = 0; i < magazine.length(); i++) { letters[magazine.charAt(i) - a]++; } for (int i = 0; i < ransomNote.length(); i++) { if (--letters[ransomNote.charAt(i) - a] < 0) { return false; } } return true; } }
RansomNoteSolution { static boolean canConstruct(String ransomNote, String magazine) { int[] letters = new int[26]; int a = 'a'; for (int i = 0; i < magazine.length(); i++) { letters[magazine.charAt(i) - a]++; } for (int i = 0; i < ransomNote.length(); i++) { if (--letters[ransomNote.charAt(i) - a] < 0) { return false; } } return true; }  }
RansomNoteSolution { static boolean canConstruct(String ransomNote, String magazine) { int[] letters = new int[26]; int a = 'a'; for (int i = 0; i < magazine.length(); i++) { letters[magazine.charAt(i) - a]++; } for (int i = 0; i < ransomNote.length(); i++) { if (--letters[ransomNote.charAt(i) - a] < 0) { return false; } } return true; }   }
RansomNoteSolution { static boolean canConstruct(String ransomNote, String magazine) { int[] letters = new int[26]; int a = 'a'; for (int i = 0; i < magazine.length(); i++) { letters[magazine.charAt(i) - a]++; } for (int i = 0; i < ransomNote.length(); i++) { if (--letters[ransomNote.charAt(i) - a] < 0) { return false; } } return true; }    }