Skip to content

Commit 0ea9827

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 635_Design_Log_Storage_System.java
1 parent 037d090 commit 0ea9827

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class LogSystem {
2+
private Map<String, Integer> delimiterMap;
3+
private TreeMap<String, List<Integer>> logs;
4+
5+
public LogSystem() {
6+
delimiterMap = new HashMap<>();
7+
logs = new TreeMap<>();
8+
9+
delimiterMap.put("Year", 4);
10+
delimiterMap.put("Month", 7);
11+
delimiterMap.put("Day", 10);
12+
delimiterMap.put("Hour", 13);
13+
delimiterMap.put("Minute", 16);
14+
delimiterMap.put("Second", 19);
15+
}
16+
17+
public void put(int id, String timestamp) {
18+
logs.putIfAbsent(timestamp, new ArrayList<>());
19+
logs.get(timestamp).add(id);
20+
}
21+
22+
public List<Integer> retrieve(String start, String end, String granularity) {
23+
List<Integer> result = new ArrayList<>();
24+
25+
int idx = delimiterMap.get(granularity);
26+
27+
String searchStart = start.substring(0, idx);
28+
String searchEnd = end.substring(0, idx);
29+
30+
for (String timestamp : logs.keySet()) {
31+
String tSubstring = timestamp.substring(0, idx);
32+
33+
if (tSubstring.compareTo(searchStart) >= 0 && tSubstring.compareTo(searchEnd) <= 0) {
34+
result.addAll(logs.get(timestamp));
35+
}
36+
}
37+
38+
return result;
39+
}
40+
}

0 commit comments

Comments
 (0)