Hash Tables: Ransom Note

  • + 8 comments

    why use complex data structures ? just continue with the given snap of code using arrays...

    1. Sort to accelerate search

    Arrays.sort(magazine); Arrays.sort(ransom);

    1. Extract to a list to benefit from the remove method

    List magazine_words = new ArrayList(Arrays.asList(magazine));

    1. Search and remove

    for (String ransom_w : ransom) { if (!magazine_words.remove(ransom_w)) { System.out.println("No"); return ; } } System.out.println("Yes");