Skip to content

Commit c7f73d7

Browse files
committed
771_Jewels_and_Stones and 804_Unique_Morse_Code_Words
1 parent 46b842a commit c7f73d7

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Remember solutions are only solutions to given problems. If you want full study
132132
| 443 | [String Compression](https://leetcode.com/problems/string-compression/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/443_String_Compression.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/443_String_Compression.java) | Maintain curr, read, write and anchor (start of this char). |
133133
| 453 | [Number of Segments in a String](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/453_Minimum_Moves_to_Equal_Array_Elements.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/453_Minimum_Moves_to_Equal_Array_Elements.java) | Each move is equal to minus one element in array, so the answer is the sum of all elements after minus min. |
134134
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/463_Island_Perimeter.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/463_Island_Perimeter.java) | math, find the area, actual number, then find the digit |
135+
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
136+
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | String, Hash and Set. Set is recommended. |
135137

136138
| # | To Understand |
137139
|---| ----- |

java/771_Jewels_and_Stones.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
public int numJewelsInStones(String J, String S) {
5+
int result = 0;
6+
HashSet jHash = new HashSet<>();
7+
for (int j = 0; j < J.length(); j++) {
8+
jHash.add(J.charAt(j));
9+
}
10+
for (int s = 0; s < S.length(); s++) {
11+
if (jHash.contains(S.charAt(s))) {
12+
result++;
13+
}
14+
}
15+
return result;
16+
}
17+
}

java/804_Unique_Morse_Code_Words.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
// https://leetcode.com/problems/unique-morse-code-words/solution/
3+
public int uniqueMorseRepresentations(String[] words) {
4+
String[] MORSE = new String[]{".-","-...","-.-.","-..",".","..-.","--.",
5+
"....","..",".---","-.-",".-..","--","-.",
6+
"---",".--.","--.-",".-.","...","-","..-",
7+
"...-",".--","-..-","-.--","--.."};
8+
9+
Set<String> seen = new HashSet();
10+
for (String word: words) {
11+
StringBuilder code = new StringBuilder();
12+
for (char c: word.toCharArray())
13+
code.append(MORSE[c - 'a']);
14+
seen.add(code.toString());
15+
}
16+
17+
return seen.size();
18+
}
19+
}

python/771_Jewels_and_Stones.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def numJewelsInStones(self, J, S):
3+
"""
4+
:type J: str
5+
:type S: str
6+
:rtype: int
7+
"""
8+
if len(J) == 0 or len(S) == 0:
9+
return 0
10+
j_set = set(J)
11+
ans = 0
12+
for c in S:
13+
if c in j_set:
14+
ans += 1
15+
return ans

python/804_Unique_Morse_Code_Words.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Morse_tab = [".-","-...","-.-.",
2+
"-..",".","..-.","--.","....",
3+
"..",".---","-.-",".-..","--",
4+
"-.","---",".--.","--.-",".-.",
5+
"...","-","..-","...-",".--",
6+
"-..-","-.--","--.."]
7+
8+
class Solution(object):
9+
# https://leetcode.com/problems/unique-morse-code-words/solution/
10+
def uniqueMorseRepresentations(self, words):
11+
"""
12+
:type words: List[str]
13+
:rtype: int
14+
"""
15+
if len(words) == 0:
16+
return 0
17+
ans_set = set()
18+
for word in words:
19+
morsed = ""
20+
for c in word:
21+
morsed += Morse_tab[ord(c) - ord('a')]
22+
23+
ans_set.add(morsed)
24+
return len(ans_set)

0 commit comments

Comments
 (0)