1239. Maximum Length of a Concatenated String with Unique Characters
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
private int maxLen = 0;
public int maxLength(List<String> words) {
if(words != null && !words.isEmpty()) {
dfs(words, 0, "");
}
return maxLen;
}
private void dfs(List<String> words, int index, String word) {
if(index > words.size()) {
return;
}
if(isUniqueCharsInStr(word)) {
maxLen = Math.max(maxLen, word.length());
} else {
return;
}
for(int i = index; i < words.size(); i++) {
dfs(words, i+1, word + words.get(i));
}
}
private boolean isUniqueCharsInStr(String s) {
if(s.length() < 2) {
return true;
}
Set<Character> set = new HashSet();
for(char ch: s.toCharArray()) {
if(set.contains(ch)) {
return false;
}
set.add(ch);
}
return true;
}
}