[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์คํ์ฑํ ๋ฐฉ
1. ๋ฌธ์ : Link
๋ฌธ์์ด command / id / nickname์ด ๋ ๋ฐฐ์ด์ด input์ผ๋ก ์ฃผ์ด์ง๊ณ , ํด๋น ๋ฐฐ์ด์ ํ๋์ฉ ํ์ธํ๋ฉด์ command์ ๋ฐ๋ผ ์ฑํ ์ฅ ์๋ฆผ์ ๋ฟ๋ ค์ฃผ๋๋ฐ ์ด๋ ๋๋ค์์ด ๋ณ๊ฒฝ๋ ์ ๋ณ๊ฒฝ๋ ๋๋ค์์ด ์ด์ ๊ณต์ง์ ๋ฐ์๋๊ฒ๋ ํ๋ ์ฝ๋
2. ํ์ด
๋จผ์ ๋ฌธ์์ด์ ๋๋ ๋ด๊ณ cmd์ ๋ฐ๋ผ nickname์ ์ ๋ฐ์ดํธ ํด์ค๋ค.
๊ทธ ๋ค์ ๊ณต์ง์ฌํญ ๋ฟ๋ คใ ใด๋ค.
3. ์ฝ๋
def solution(record):
answer = []
idic = {}
for re in record:
relist = re.split()
cmd = relist[0]
if cmd == 'Enter' or cmd == 'Change':
idd = relist[1]
nick = relist[2]
idic[idd] = nick
for re in record:
relist = re.split()
cmd = relist[0]
if cmd == 'Enter':
idd = relist[1]
answer.append(idic[idd] + '๋์ด ๋ค์ด์์ต๋๋ค.')
elif cmd == 'Leave':
idd = relist[1]
answer.append(idic[idd] + '๋์ด ๋๊ฐ์ต๋๋ค.')
return answer
import java.util.*;
class Solution {
public String[] solution(String[] record) {
Map<String, String> hm = new HashMap<String, String>();
for(String re : record){
String[] reString = re.split(" ");
String order = reString[0];
String id = reString[1];
if(order.equals("Enter")){
String nick = reString[2];
hm.put(id, nick);
} else if(order.equals("Change")){
String nick = reString[2];
hm.put(id, nick);
}
}
List<String> an = new ArrayList<String>();
for(String re : record){
String[] reString = re.split(" ");
String order = reString[0];
String id = reString[1];
if(order.equals("Enter")){
String nick = hm.get(id);
String message = nick + "๋์ด ๋ค์ด์์ต๋๋ค.";
an.add(message);
} else if(order.equals("Leave")){
String nick = hm.get(id);
String message = nick + "๋์ด ๋๊ฐ์ต๋๋ค.";
an.add(message);
}
}
String[] answer = new String[an.size()];
for(int i=0; i<an.size(); i++){
answer[i] = an.get(i);
}
return answer;
}
}
'๐ป Coding Problems Solving > Array | String | Loop' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์์ด ๋๋ง์๊ธฐ (0) | 2022.04.08 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๊ฐ์ฅ ํฐ ์ (0) | 2022.04.07 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๊ธฐ๋ฅ๊ฐ๋ฐ (0) | 2022.04.07 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 124 ๋๋ผ์ ์ซ์ (0) | 2022.04.07 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฌธ์์ด ์์ถ (0) | 2022.04.07 |
์ต๊ทผ๋๊ธ