Skip to content

Commit 10885cb

Browse files
committed
Added 3 solutions
1 parent 848c9ab commit 10885cb

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Easy/Letter Case Permutation.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public List<String> letterCasePermutation(String S) {
3+
List<String> ans = new ArrayList<>();
4+
letterCasePermutationHelper(ans, S.toCharArray(), 0);
5+
6+
return ans;
7+
}
8+
9+
private void letterCasePermutationHelper(List<String> ans, char[] chars, int index) {
10+
if (index == chars.length) {
11+
ans.add(new String(chars));
12+
}
13+
else {
14+
if (Character.isLetter(chars[index])) {
15+
chars[index] = Character.toLowerCase(chars[index]);
16+
letterCasePermutationHelper(ans, chars, index+1);
17+
chars[index] = Character.toUpperCase(chars[index]);
18+
}
19+
20+
letterCasePermutationHelper(ans, chars, index+1);
21+
}
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public static int[] numberOfLines(int[] widths, String S) {
3+
if (S.isEmpty()) {
4+
return new int[]{0,0};
5+
}
6+
7+
int capacity = 100;
8+
char[] chars = S.toCharArray();
9+
int count = 1;
10+
11+
for (char c : chars) {
12+
if (capacity - widths[c-'a'] < 0) {
13+
capacity = 100;
14+
count++;
15+
}
16+
capacity -= widths[c-'a'];
17+
}
18+
19+
return new int[]{count, 100-capacity};
20+
}
21+
}

Medium/Contiguous Array.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int findMaxLength(int[] nums) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
map.put(0, -1);
5+
int maxLen = 0;
6+
int count = 0;
7+
8+
for (int i=0; i<nums.length; i++) {
9+
count += nums[i] == 0 ? -1 : 1;
10+
if (map.containsKey(count)) {
11+
maxLen = Math.max(maxLen, i-map.get(count));
12+
}
13+
else {
14+
map.put(count, i);
15+
}
16+
}
17+
18+
return maxLen;
19+
}
20+
}

0 commit comments

Comments
 (0)