class Solution { private int res = 0; public int maxLength(List<String> arr) { if (arr == null || arr.size() == 0) return 0; dfs(arr, "", 0); return res; } public void dfs(List<String> arr, String path, int index) { boolean unique = isUnique(path); // dfs: length now: unique or not if (unique) res = Math.max(res, path.length()); // recursion ends; not unique if (index == arr.size() || !unique) return ; for (int i = index; i < arr.size(); ++i) { // dfs: append next string to current path dfs(arr, path + arr.get(i), i + 1); } } public boolean isUnique(String s) { Set<Character> set = new HashSet<>(); for (char c : s.toCharArray()) { if (set.contains(c)) return false; set.add(c); } return true; } }