Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2ec3139

Browse files
add 1160
1 parent 5b6a82e commit 2ec3139

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java) | O(n) | O(m) | |Easy||
3031
|5087|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5087.java) | O(1) | O(1) | |Medium||
3132
|5083|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5083.java) | O(n) | O(1) | |Easy||
3233
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy||
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 1160. Find Words That Can Be Formed by Characters
8+
*
9+
* You are given an array of strings words and a string chars.
10+
* A string is good if it can be formed by characters from chars (each character can only be used once).
11+
* Return the sum of lengths of all good strings in words.
12+
*
13+
* Example 1:
14+
*
15+
* Input: words = ["cat","bt","hat","tree"], chars = "atach"
16+
* Output: 6
17+
* Explanation:
18+
* The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
19+
*
20+
* Example 2:
21+
*
22+
* Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
23+
* Output: 10
24+
* Explanation:
25+
* The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
26+
*
27+
* Note:
28+
*
29+
* 1 <= words.length <= 1000
30+
* 1 <= words[i].length, chars.length <= 100
31+
* All strings contain lowercase English letters only.
32+
* */
33+
public class _1160 {
34+
public static class Solution1 {
35+
public int countCharacters(String[] words, String chars) {
36+
int length = 0;
37+
Map<Character, Integer> map = new HashMap<>();
38+
for (char c : chars.toCharArray()) {
39+
int count = map.getOrDefault(c, 0);
40+
map.put(c, count + 1);
41+
42+
}
43+
for (String word : words) {
44+
if (canForm(word, map)) {
45+
length += word.length();
46+
}
47+
}
48+
return length;
49+
}
50+
51+
private boolean canForm(String word, final Map<Character, Integer> map) {
52+
Map<Character, Integer> tmp = new HashMap<>(map);
53+
for (Character c : word.toCharArray()) {
54+
if (tmp.containsKey(c) && tmp.get(c) > 0) {
55+
int count = tmp.get(c);
56+
tmp.put(c, count - 1);
57+
} else {
58+
return false;
59+
}
60+
}
61+
return true;
62+
}
63+
64+
}
65+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1160;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1160Test {
10+
private static _1160.Solution1 solution1;
11+
private static String[] words;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1160.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
words = new String[]{"cat", "bt", "hat", "tree"};
21+
assertEquals(6, solution1.countCharacters(words, "atach"));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
words = new String[]{"hello", "world", "leetcode"};
27+
assertEquals(10, solution1.countCharacters(words, "welldonehoneyr"));
28+
}
29+
}

0 commit comments

Comments
 (0)