본문 바로가기

Algorithm

[leetcode] 588. Design In-Memory File System

Design an in-memory file system to simulate the following functions:

ls: Given a path in string format. If it is a file path, return a list that only contains this file's name. If it is a directory path, return the list of file and directory names in this directory. Your output (file and directory names together) should in lexicographic order.

 

mkdir: Given a directory path that does not exist, you should make a new directory according to the path. If the middle directories in the path don't exist either, you should create them as well. This function has void return type.

addContentToFile: Given a file path and file content in string format. If the file doesn't exist, you need to create that file containing given content. If the file already exists, you need to append given content to original content. This function has void return type.

readContentFromFile: Given a file path, return its content in string format.

 

Example:

Input: ["FileSystem","ls","mkdir","addContentToFile","ls","readContentFromFile"] [[],["/"],["/a/b/c"],["/a/b/c/d","hello"],["/"],["/a/b/c/d"]] Output: [null,[],null,null,["a"],"hello"] Explanation:

Note:

  1. You can assume all file or directory paths are absolute paths which begin with / and do not end with / except that the path is just "/".
  2. You can assume that all operations will be passed valid parameters and users will not attempt to retrieve file content or list a directory or file that does not exist.
  3. You can assume that all directory names and file names only contain lower-case letters, and same names won't exist in the same directory.

leetcode 내에서 난이도 hard 의 문제로 In-Memory file system 을 만드는 문제이다.

 

나는 trie 자료 구조로 문제를 풀었는데, 아래 소스에서 content 가 null 인 경우는 디렉토리 노드를, content 가 null 이 아닌 경우는 file node 를 나타낸다.

 

아래의 소스에서 각 메소드의 시간 복잡도는 다음과 같다.

  • ls : path 에 해당되는 트리의 깊이를 N, 해당 깊이에서 가지고 있는 children 의 수를 M 이라고 할때, $O(NM)$ 이 된다.
  • mkdir : $O(N)$
  • addContentToFile : $O(N)$
  • readContentFromFile : $O(N)$
class FileSystem {

    static class Node {
			TreeMap<String, Node> children = new TreeMap<>();
			String val;
			String content;

			public Node(String val, String content) {
				this.val = val;
				this.content = content;
			}
		}

		Node root;

		public FileSystem() {
			this.root = new Node("/", null);
		}

		public List<String> ls(String path) {
			Node cur = this.root;

			String [] paths = path.split("/");

			for (int i = 1; i < paths.length; i++) {
				cur = cur.children.get(paths[i]);
			}

			List<String> res = new ArrayList<>();

			if (cur.content != null) {
				res.add(cur.val);
			} else {
				for (String curPath : cur.children.keySet()) {
					res.add(curPath);
				}
			}

			return res;
		}


		public void mkdir(String path) {
			Node cur = this.root;
			String [] paths = path.split("/");

			for (int i = 1; i < paths.length; i++) {
				String dir = paths[i];

				if (cur.children.get(dir) == null) {
					cur.children.put(dir, new Node(dir, null));
				}

				cur = cur.children.get(dir);
			}
		}

		public void addContentToFile(String filePath, String content) {
			Node cur = this.root;
			String [] paths = filePath.split("/");

			for (int i = 1; i < paths.length; i++) {
				String dirOrFile = paths[i];

				if (i == paths.length - 1) {
					cur.children.computeIfAbsent(dirOrFile, t -> new Node(dirOrFile, "")).content += content;
				} else {
					cur = cur.children.get(dirOrFile);
				}
			}
		}

		public String readContentFromFile(String filePath) {
			Node cur = this.root;
			String [] paths = filePath.split("/");

			for (int i = 1; i < paths.length; i++) {
				String dirOrFile = paths[i];

				if (i == paths.length - 1) {
					return cur.children.get(dirOrFile).content;
				} else {
					cur = cur.children.get(dirOrFile);
				}
			}

			return "";
		}
}