str1 | str2 | result |
---|---|---|
"ab6CDE443fgh22iJKlmn1o" | "6CD" | 1 |
"ppprrrogrammers" | "pppp" | 2 |
"AbcAbcA"AAA" | "AAA" | 2 |
๋์ ํ์ด
class Solution {
public int solution(String str1, String str2) {
int answer = 0;
if(str1.contains(str2) == true){
answer = 1;
}else{
answer = 2;
}
return answer;
}
}
๋์ ์๊ฐ
๋ฌธ์์ด contains method
๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๊ฒ ํ๋ณ๊ฐ๋ฅ
my_string | result |
---|---|
"aAb1B2cC34oOp" | 10 |
"1a2b3c4d123" | 16 |
์
์ถ๋ ฅ ์
"aAb1B2cC34oOp"์์ ํ์๋ฆฌ ์์ฐ์๋ 1, 2, 3, 4 ์ ๋๋ค. ๋ฐ๋ผ์ 1 + 2 + 3 + 4 = 10 ์ returnํฉ๋๋ค.
"1a2b3c4d123Z"์์ ํ์๋ฆฌ ์์ฐ์๋ 1, 2, 3, 4, 1, 2, 3 ์ ๋๋ค. ๋ฐ๋ผ์ 1 + 2 + 3 + 4 + 1 + 2 + 3 = 16 ์ returnํฉ๋๋ค.
๋์ ํ์ด
class Solution {
public int solution(String my_string) {
int answer = 0;
for (int i = 0; i<my_string.length();i++) {
if(my_string.charAt(i)>='0' && my_string.charAt(i)<='9') {
answer += my_string.charAt(i) - '0';
}
}
return answer;
}
}
๋์ ์๊ฐ
์์คํค์ฝ๋ ํ ์, 10์ง 48~57๋ฒ
๊น์ง ,๋ฌธ์ '0'~'9'
๊น์ง๋ฅผ ๋งค๊ฐ๋ณ์ my_string์์ ๊ฒ์ถํ์ฌ intํ ๋ณ์ answer์ ๋ํ์ฌ ๋ฆฌํด
my_string | result |
---|---|
"bus" | "bs |
"nice to meet you" | "nc t mt y" |
์
์ถ๋ ฅ ์
๋์ ํ์ด
class Solution {
public static String solution(String my_string) {
String answer = my_string.replaceAll("[aeiou]", "");
System.out.println(answer);
return answer;
}
}
๋์ ์๊ฐ
String์ replaceAll
method๋ฅผ ์ฌ์ฉํ์ฌ, ๊ดํธ()
์ฒซ๋ฒ์งธ์๋ ์ ๊ท์, ๋๋ฒ์งธ๋ ๋ฐ๊ฟ ๋ฌธ์๋ฅผ ๋ฃ๋ ๋ฐฉ๋ฒ์ ์ด์ฉํ์ฌ ๋ฌธ์ ์ ์ ๊ทผ, ํน์ ๋ฌธ์๋ฅผ ์ ๊ท์์ผ๋ก ๋ฃ์ด, ํน์ ๋ฌธ์๊ฐ ํฌํจ๋๋ฉด ""
์ผ๋ก ํด๋น ๋ฌธ์๋ฅผ ์ ๊ฑฐ