Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- dynamic programming
- sorting
- hash table
- Class
- two pointers
- Matrix
- Counting
- 코테
- Number Theory
- 자바
- greedy
- 파이썬
- Binary Tree
- database
- SQL
- Binary Search
- string
- Tree
- geometry
- simulation
- 구현
- 코딩테스트
- Data Structure
- implement
- bit manipulation
- java
- Math
- array
- Method
- Stack
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 804. Unique Morse Code Words 본문
1. Input
1) 문자열 배열 words
2. Output
1) words의 모든 문자열을 모스 부호로 바꿨을 때 나올 수 있는 모스 부호의 수
3. Constraint
1) 1 <= words.length <= 100
2) 1 <= words[i].length <= 12
3) words[i]는 영어 소문자로만 이루어져 있다.
4. Example
Input: words = {"gin","zen","gig","msg"} -> Output: 2
설명: 각 단어를 모스부호로 바꿔보면
- "gin" -> "--...-."
- "zen" -> "--...-." -> “gin”과 같음
- "gig" -> "--...--."
- "msg" -> "--...--." -> “gig”와 같음
"--...-.“와 "--...--.". 2가지 모스 부호가 있으므로 2를 반환한다.
5. Code
1) 첫 코드(2022/06/14)
if(words.length == 1) return 1;
String[] tf = new String[words.length];
String[] code = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; // 문제에서 제공해준 것
String s = "";
for(int i=0 ; i<tf.length ; i++){
for(int j=0 ; j<words[i].length() ; j++)
s += code[words[i].charAt(j)-97];
tf[i] = s;
s = "";
}
List<String> unique = new ArrayList();
unique.add(tf[0]);
for(int i=1 ; i<tf.length ; i++){
if(!unique.contains(tf[i]))
unique.add(tf[i]);
} // for i
return unique.size();
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 821. Shortest Distance to a Character (0) | 2022.12.08 |
---|---|
[LeetCode/Easy] 806. Number of Lines To Write String (0) | 2022.12.07 |
[LeetCode/Easy] 796. Rotate String (0) | 2022.12.07 |
[LeetCode/Easy] 771. Jewels and Stones (0) | 2022.12.07 |
[LeetCode/Easy] 762. Prime Number of Set Bits in Binary Representation (0) | 2022.12.07 |