Skip to content

Commit 79aafa2

Browse files
committed
leetcode
1 parent e860b0a commit 79aafa2

4 files changed

+496
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
3+
4+
-* Maximum Length of a Concatenated String with Unique Characters *-
5+
6+
You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.
7+
8+
Return the maximum possible length of s.
9+
10+
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
11+
12+
13+
14+
Example 1:
15+
16+
Input: arr = ["un","iq","ue"]
17+
Output: 4
18+
Explanation: All the valid concatenations are:
19+
- ""
20+
- "un"
21+
- "iq"
22+
- "ue"
23+
- "uniq" ("un" + "iq")
24+
- "ique" ("iq" + "ue")
25+
Maximum length is 4.
26+
Example 2:
27+
28+
Input: arr = ["cha","r","act","ers"]
29+
Output: 6
30+
Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").
31+
Example 3:
32+
33+
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
34+
Output: 26
35+
Explanation: The only string in arr has all 26 characters.
36+
37+
38+
Constraints:
39+
40+
1 <= arr.length <= 16
41+
1 <= arr[i].length <= 26
42+
arr[i] contains only lowercase English letters.
43+
44+
45+
*/
46+
import 'dart:collection';
47+
import 'dart:math';
48+
49+
class A {
50+
// BackTracking
51+
//Runtime: 589 ms, faster than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
52+
// Memory Usage: 148.7 MB, less than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
53+
54+
int max = 0;
55+
int maxLength(List<String> arr) {
56+
backTrack(arr, "", 0);
57+
return max;
58+
}
59+
60+
void backTrack(List<String> arr, String current, int start) {
61+
if (max < current.length) max = current.length;
62+
for (int i = start; i < arr.length; i++) {
63+
if (!isValid(current, arr.elementAt(i))) continue;
64+
backTrack(arr, current + arr.elementAt(i), i + 1);
65+
}
66+
}
67+
68+
bool isValid(String currentString, String newString) {
69+
List<int> array = List.filled(26, 0);
70+
for (int i = 0; i < newString.length; i++) {
71+
if (++array[newString.codeUnitAt(i) - 'a'.codeUnitAt(0)] == 2)
72+
return false;
73+
if (currentString.contains(
74+
newString[i] + "",
75+
)) return false;
76+
}
77+
return true;
78+
}
79+
}
80+
81+
class B {
82+
// Recursion
83+
// Runtime: 779 ms, faster than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
84+
// Memory Usage: 173.7 MB, less than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
85+
86+
int maxLength(List<String> arr) {
87+
return solve(arr, 0, "");
88+
}
89+
90+
int solve(List<String> arr, int i, String soFar) {
91+
if (i == arr.length) {
92+
if (isValid(soFar)) {
93+
return soFar.length;
94+
}
95+
return 0;
96+
}
97+
98+
int size1 = solve(arr, i + 1, soFar);
99+
int size2 = solve(arr, i + 1, soFar + arr.elementAt(i));
100+
101+
return max(size1, size2);
102+
}
103+
104+
bool isValid(String s) {
105+
List<int> freq = List.filled(26, 0);
106+
107+
for (int i = 0; i < s.length; i++) {
108+
int val = s.codeUnitAt(i) - 'a'.codeUnitAt(0);
109+
freq[val]++;
110+
111+
if (freq[val] > 1) {
112+
return false;
113+
}
114+
}
115+
116+
return true;
117+
}
118+
}
119+
120+
class C {
121+
// Depth First Search
122+
// Runtime: 598 ms, faster than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
123+
// Memory Usage: 152.3 MB, less than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
124+
int result = 0;
125+
int maxLength(List<String> arr) {
126+
if (arr.isEmpty || arr.length == 0) {
127+
return 0;
128+
}
129+
130+
dfs(arr, "", 0);
131+
132+
return result;
133+
}
134+
135+
void dfs(List<String> arr, String path, int idx) {
136+
bool isUniqueChar = isUniqueChars(path);
137+
138+
if (isUniqueChar) {
139+
result = max(path.length, result);
140+
}
141+
142+
if (idx == arr.length || !isUniqueChar) {
143+
return;
144+
}
145+
146+
for (int i = idx; i < arr.length; i++) {
147+
dfs(arr, path + arr.elementAt(i), i + 1);
148+
}
149+
}
150+
151+
bool isUniqueChars(String s) {
152+
HashSet<String> hashset = HashSet();
153+
154+
for (String c in s.split("")) {
155+
if (hashset.contains(c)) {
156+
return false;
157+
}
158+
hashset.add(c);
159+
}
160+
return true;
161+
}
162+
}
163+
164+
class D {
165+
// BitMask
166+
// Runtime: 479 ms, faster than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
167+
// Memory Usage: 141.2 MB, less than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
168+
int maxLength(List<String> arr) {
169+
int n = arr.length;
170+
List<int> ans = List.filled(1, 0);
171+
List<List<int>> bitMap =
172+
List.filled(n, 0).map((e) => List.filled(2, 0)).toList();
173+
for (int i = 0; i < n; ++i) {
174+
int a = 0;
175+
for (String ch in arr.elementAt(i).split("")) {
176+
int bit = ch.codeUnitAt(0) - 'a'.codeUnitAt(0);
177+
if (((a >> bit) & 1) == 1) {
178+
a = 0;
179+
break;
180+
}
181+
a |= (1 << bit);
182+
}
183+
bitMap[i][0] = a;
184+
bitMap[i][1] = a == 0 ? 0 : arr.elementAt(i).length;
185+
}
186+
dfs(arr, bitMap, ans, 0, 0, 0);
187+
return ans[0];
188+
}
189+
190+
void dfs(List<String> arr, List<List<int>> bitMap, List<int> ans, int start,
191+
int curLen, int curBit) {
192+
if (ans[0] < curLen) ans[0] = curLen;
193+
for (int i = start; i < arr.length; ++i) {
194+
if ((curBit & bitMap[i][0]) != 0) continue;
195+
dfs(arr, bitMap, ans, i + 1, curLen + bitMap[i][1],
196+
curBit | bitMap[i][0]);
197+
}
198+
}
199+
}
200+
201+
class E {
202+
// Runtime: 2126 ms, faster than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
203+
// Memory Usage: 207.2 MB, less than 100.00% of Dart online submissions for Maximum Length of a Concatenated String with Unique Characters.
204+
205+
int maxLength(List<String> arr) {
206+
List<int> result = List.filled(1, 0);
207+
maxLengthUnique(arr, result, 0, "");
208+
return result[0];
209+
}
210+
211+
void maxLengthUnique(
212+
List<String> arr, List<int> result, int index, String current) {
213+
int i = 0, n = arr.length;
214+
215+
// We use queue to store all the combinations possible!
216+
Queue<String> q = Queue();
217+
q.add("");
218+
219+
//Loop for all the strings in array.
220+
while (i < n) {
221+
int size = q.length;
222+
223+
//Loop till all the elements in Queue are appended with "" and the string 'str'
224+
for (int loop = 0; loop < size; loop++) {
225+
String temp = q.removeFirst(); // temp = ""
226+
String dontConsider = temp; // dontConsider = ""
227+
String consider = temp +
228+
arr.elementAt(
229+
i); // consider = "un" --> Aboe 3 steps Repeated till the last element.
230+
231+
// Function call to calculate the unique characters
232+
if (uniqueCharacters(dontConsider) > result[0]) {
233+
result[0] = dontConsider.length;
234+
}
235+
// Function call to calculate the unique characters
236+
if (uniqueCharacters(consider) > result[0]) {
237+
result[0] = consider.length;
238+
}
239+
// Adding the new strings to our queue again.
240+
q.add(dontConsider);
241+
q.add(consider);
242+
}
243+
i++;
244+
}
245+
}
246+
247+
int uniqueCharacters(String string) {
248+
List<int> alpha = List.filled(26, 0);
249+
250+
for (String character in string.split(""))
251+
if (alpha[character.codeUnitAt(0) - 'a'.codeUnitAt(0)]++ > 0) return -1;
252+
253+
return string.length;
254+
}
255+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main

0 commit comments

Comments
 (0)