From 32aae472a33649bb30c56845a6510b8cb4752c62 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Sun, 17 Sep 2017 00:26:47 -0500 Subject: [PATCH] Create 588.Design-In-Memory-File-System.cpp --- .../588.Design-In-Memory-File-System.cpp | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Trie/588.Design-In-Memory-File-System/588.Design-In-Memory-File-System.cpp diff --git a/Trie/588.Design-In-Memory-File-System/588.Design-In-Memory-File-System.cpp b/Trie/588.Design-In-Memory-File-System/588.Design-In-Memory-File-System.cpp new file mode 100644 index 000000000..2cef7592f --- /dev/null +++ b/Trie/588.Design-In-Memory-File-System/588.Design-In-Memory-File-System.cpp @@ -0,0 +1,86 @@ +class FileSystem { + class TrieNode + { + public: + mapMap; + bool isFile; + }; + TrieNode* root; + unordered_mapFileContents; + +public: + FileSystem() + { + root=new TrieNode(); + } + + vector ls(string path) + { + TrieNode* node=root; + string str; + for (int i=1; iMap[str]; + } + + if (node->isFile) + return {str}; + else + { + vectorresults; + for (auto a:node->Map) + results.push_back(a.first); + return results; + } + } + + void mkdir(string path) + { + TrieNode* node=root; + for (int i=1; iMap.find(str)==node->Map.end()) + node->Map[str]=new TrieNode(); + node=node->Map[str]; + } + } + + void addContentToFile(string filePath, string content) + { + TrieNode* node=root; + for (int i=1; iMap.find(str)==node->Map.end()) + node->Map[str]=new TrieNode(); + node=node->Map[str]; + } + node->isFile=1; + FileContents[filePath]+=content; + } + + string readContentFromFile(string filePath) + { + return FileContents[filePath]; + } +}; + +/** + * Your FileSystem object will be instantiated and called as such: + * FileSystem obj = new FileSystem(); + * vector param_1 = obj.ls(path); + * obj.mkdir(path); + * obj.addContentToFile(filePath,content); + * string param_4 = obj.readContentFromFile(filePath); + */