Print Java Packages as Indented Tree
viaLeetCode
Problem Given fully-qualified Java names (e.g. java.util.vector, java.util.Date, org.json.JSONObject, java.util.regex.Pattern), print them as an indented tree grouped by package segment.
Input / Output
- Input: list of dotted names.
- Output: printed hierarchy, one segment per line, children indented under parents (e.g. java → util → {vector, Date, regex → Pattern}).
Constraints
- Up to 10^4 names; clarify sibling ordering (alphabetical vs insertion) and duplicate handling.
Example
- [java.util.vector, java.util.Date, org.json.JSONObject] → java / (indent) util / (indent×2) vector, Date; org / (indent) json / (indent×2) JSONObject.
Expected approach
- Build a trie of segments: nested Map<String, Node>, inserting each name split on '.'. DFS the trie printing segment names with depth-proportional indentation (preorder). O(total segments) build and print. Talking points: TreeMap for sorted output, marking terminal nodes if classes vs packages must be distinguished.
asked …