LeetCode - Implement Magic Dictionary

Question Definition

Implement a magic directory with buildDict, and search methods.

For the method buildDict, you’ll be given a list of non-repetitive words to build a dictionary.

For the method search, you’ll be given a word, and judge whether if you modify exactly one character into another character in this word, the modified word is in the dictionary you just built.

Example 1:

Input: buildDict(["hello", "leetcode"]), Output: Null
Input: search("hello"), Output: False
Input: search("hhllo"), Output: True
Input: search("hell"), Output: False
Input: search("leetcoded"), Output: False

Note:

  1. You may assume that all the inputs are consist of lowercase letters a-z.
  2. For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.
  3. Please remember to RESET your class variables declared in class MagicDictionary, as static/class variables are persisted across multiple test cases. Please see here for more details.

    Java Solution

    class MagicDictionary {
     Map<Integer, List<String>> map;
     /** Initialize your data structure here. */
     public MagicDictionary() {
         map = new HashMap<>();
     }
    
     /** Build a dictionary through a list of words */
     public void buildDict(String[] dict) {
         for(String d : dict){
             map.putIfAbsent(d.length(), new LinkedList<>());
             map.get(d.length()).add(d);
         }
     }
    
     /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
     public boolean search(String word) {
         if(!map.containsKey(word.length()))
             return false;
         for(String s : map.get(word.length())){
             boolean diff = false;
             int i = 0;
             for(; i < s.length(); i++){
                 if(s.charAt(i) != word.charAt(i)){
                     if(diff){
                         break;
                     }else{
                         diff = true;
                     }
                 }
             }
             if(diff && i == s.length())
                 return true;
         }
         return false;
     }
    }
    

LeetCode - Find Duplicate File in System

Question Definition

Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their paths.

A group of duplicate files consists of at least two files that have exactly the same content.

A single directory info string in the input list has the following format:

“root/d1/d2/…/dm f1.txt(f1_content) f2.txt(f2_content) … fn.txt(fn_content)”

It means there are n files (f1.txt, f2.txt … fn.txt with content f1_content, f2_content … fn_content, respectively) in directory root/d1/d2/…/dm. Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.

The output is a list of group of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:

“directory_path/file_name.txt”

More …

LeetCode - Daily Temperatures

Question Definition

Given a list of daily temperatures, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

More …

LeetCode - Brick Wall

Question Definition

There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to draw a vertical line from the top to the bottom and cross the least bricks.

The brick wall is represented by a list of rows. Each row is a list of integers representing the width of each brick in this row from left to right.

If your line go through the edge of a brick, then the brick is not considered as crossed. You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks.

You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.

More …