class Solution {
public String solution(String polynomial) {
String answer = "";
int x=0;
int num=0;
String tokens[] = polynomial.split("\\+");
for(String token:tokens)
{
token = token.trim();
if(token.contains("x"))
{
token = token.replace("x","");
if(token.equals("")) x++;
else x+=Integer.parseInt(token);
}
else{
num+=Integer.parseInt(token);
}
}
String strX = "";
if(x==0) return num+"";
else if(x==1) strX="x";
else strX = x+"x";
if(num==0) return strX;
else return strX+" + "+num;
}
}
'코딩테스트 > lv0' 카테고리의 다른 글
[프로그래머스 코딩테스트] java Lv.0 최빈값 구하기 (0) | 2024.08.26 |
---|---|
[프로그래머스 코딩테스트] java Lv.0 OX퀴즈 (0) | 2024.08.26 |
[프로그래머스 코딩테스트] java Lv.0 문자열 밀기 (0) | 2024.08.22 |
[프로그래머스 코딩테스트] java Lv.0 특이한 정렬 (0) | 2024.08.22 |
[프로그래머스 코딩테스트] java Lv.0 유한소수 판별하기 (0) | 2024.08.20 |