Skip to content

Commit 031a57c

Browse files
committed
Added 2 solutions
1 parent 5a932e5 commit 031a57c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Easy/Min Cost Climbing Stairs.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int minCostClimbingStairs(int[] cost) {
3+
int[] dp = new int[cost.length + 1];
4+
5+
for (int i = 2; i <= cost.length; i++)
6+
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
7+
8+
return dp[cost.length];
9+
}
10+
}

Medium/Shortest Completing Word.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public String shortestCompletingWord(String licensePlate, String[] words) {
3+
4+
String licenseWords = "";
5+
for (int i=0; i<licensePlate.length(); i++) {
6+
if (Character.isLetter(licensePlate.charAt(i))) {
7+
licenseWords = licenseWords + String.valueOf(licensePlate.charAt(i));
8+
}
9+
}
10+
11+
String ans = "";
12+
int l = Integer.MAX_VALUE;
13+
14+
for (String word : words) {
15+
if (complete(word.toLowerCase(), licenseWords.toLowerCase())) {
16+
if (word.length() < l) {
17+
l = word.length();
18+
ans = word;
19+
}
20+
}
21+
}
22+
23+
return ans;
24+
}
25+
26+
private boolean complete(String word, String toCheck) {
27+
28+
Map<Character, Integer> wordMap = new HashMap<>();
29+
Map<Character, Integer> toCheckMap = new HashMap<>();
30+
31+
for (int i=0;i<word.length(); i++) {
32+
wordMap.put(word.charAt(i), wordMap.getOrDefault(word.charAt(i), 0) + 1);
33+
}
34+
35+
for (int i=0;i<toCheck.length(); i++) {
36+
toCheckMap.put(toCheck.charAt(i), toCheckMap.getOrDefault(toCheck.charAt(i), 0) + 1);
37+
}
38+
39+
for(Map.Entry<Character, Integer> entry : toCheckMap.entrySet()) {
40+
if (wordMap.containsKey(entry.getKey()) && wordMap.get(entry.getKey()) >= entry.getValue()) {
41+
continue;
42+
}
43+
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
}

0 commit comments

Comments
 (0)