File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -22,10 +22,12 @@ public static int check(String string1, String string2) {
2222 if (string1 == null ||
2323 string2 == null ||
2424 string1 .length () != string2 .length ()) return 0 ;
25- Set <Character > characters1 = string1 .trim ().chars ().mapToObj (o -> (char ) o ).collect (Collectors .toSet ());
26- Set <Character > characters2 = string2 .trim ().chars ().mapToObj (o -> (char ) o ).collect (Collectors .toSet ());
27- for (Character character : characters2 ) {
28- if (!characters1 .contains (character )) {
25+ int [] counts = new int [Character .MAX_VALUE + 1 ];
26+ for (char c : string1 .toCharArray ()) {
27+ counts [c ]++;
28+ }
29+ for (char c : string2 .toCharArray ()) {
30+ if (--counts [c ] < 0 ) {
2931 return 0 ;
3032 }
3133 }
@@ -44,5 +46,7 @@ public void test() {
4446 assertEquals (1 , check ("" , "" ));
4547 assertEquals (0 , check ("" , "fff" ));
4648 assertEquals (0 , check ("ddd" , "" ));
49+ assertEquals (1 , check ("aab" , "aba" ));
50+ assertEquals (0 , check ("aab" , "abb" ));
4751 }
4852}
You can’t perform that action at this time.
0 commit comments