1. ๋ฌธ์์ฐพ๊ธฐ
import java.util.Scanner;
public class Main {
public int solution(String str, char t){
int answer=0;
str=str.toUpperCase();
t=Character.toUpperCase(t);
for(int i=0; i<str.length(); i++){
if(str.charAt(i)==t)
answer++;
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str = kb.next();
char c = kb.next().charAt(0);
System.out.print(T.solution(str,c));
}
}
2. ๋์๋ฌธ์ ๋ณํ
import java.util.*;
public class Main {
public String solution(String str){
String answer="";
for(char x : str.toCharArray()){
if(Character.isLowerCase(x))
answer+=Character.toUpperCase(x);
else
answer+=Character.toLowerCase(x);
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str = kb.next();
System.out.print(T.solution(str);
}
}
3. ๋ฌธ์ฅ ์ ๋จ์ด ( indextOf(), substring() )
import java.util.*;
public class Main {
public String solution(String str){
String answer="";
int m=Integer.MIN_VALUE; // ์์ ๊ฐ์ผ๋ก ์ด๊ธฐํ
String[] s=str.split(" "); // split
for(String x : s){
int len=x.length();
if(len>m){ // ์ต๋๊ฐ ์๊ณ ๋ฆฌ์ฆ
m=len;
answer=x;
}
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str = kb.nextLine();
System.out.print(T.solution(str);
}
}
4. ๋จ์ด ๋ค์ง๊ธฐ(StringBuilder ์ด์ฉ๋ฒ ๋๋ ์ง์ ๋ค์ง๊ธฐ )
import java.util.*;
public class Main {
public ArrayList<String> solution(int n, String[] str){
ArrayList<String> answer=new ArrayList<>();
for(String x:str){
String tmp=new StringBuilder(x).reverse().toString();
answer.add(tmp);
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
String[] str=new String[n];
for(int i=0; i<n; i++){
str[i]=kb.next();
}
for(String x:T.solution(n,str)){
System.out.println(x);
}
}
}
5. ํน์ ๋ฌธ์ ๋ค์ง๊ธฐ( toCharArray() )
import java.util.*;
public class Main {
public String solution(String str){
String answer;
char[] s=str.toCharArray();
int lt=0;
int rt=str.length()-1;
while(lt<rt) {
if(!Character.isAlphabetic(s[lt])) // ์ํ๋ฒณ์ด ์๋๋
lt++;
else if(!Character.isAlphabetic(s[rt])) // ์ํ๋ฒณ์ด ์๋๋
rt++;
else { // ์ํ๋ฒณ์ด๋ฉด
char tmp=s[lt];
s[lt]=s[rt];
s[rt]=tmp;
lt++;
rt--;
}
}
answer=String.valueOf(s); // line5 : ๋ฐ๊ฟจ๋ s ๋ฐฐ์ด์ ๋ค์ Stringํ
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.next();
System.out.print(T.solution(str));
}
}
6. ์ค๋ณต๋ฌธ์์ ๊ฑฐ
import java.util.*;
public class Main {
public String solution(String str){
String answer="";
for(int i=0; i<str.length(); i++){
if(str.indexOf(str.charAt(i))==i)
answer+=str.charAt(i);
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.next();
System.out.print(T.solution(str));
}
}
7. ํ๋ฌธ๋ฌธ์์ด
solution1
import java.util.*;
public class Main {
public String solution(String str){
String answer="No"; // str=์ ๋ฐฉํฅ tmp=์ญ๋ฐฉํฅ
String tmp=new StringBuilder(str).reverse().toString();
if(str.equalsIgnoreCase(tmp)) // ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ๊ฐ์์ง ํ์ธ
answer="YES";
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.next();
System.out.print(T.solution(str));
}
}
solution2
import java.util.*;
public class Main {
public String solution(String str){
String answer="YES";
str=str.toUpperCase();
int len=str.length();
for(int i=0; i<len/2; i++){
if(str.charAt(i)!=str.charAt(len-i-1))
return "No";
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.next();
System.out.print(T.solution(str));
}
}
8. ํฐ๋ฆฐ๋๋กฌ( replaceAll ์ ๊ท์์ด์ฉ )
import java.util.*;
public class Main {
public String solution(String s){
String answer="NO";
s=s.toUpperCase().replaceAll("[^A-Z]",""); //A-Z์ด ์๋๊ฒ์ "" ๋์ฒด
String tmp=new StringBuilder(s).reverse().toString();
if(s.equals(tmp)) answer="YES";
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.nextLine();
System.out.print(T.solution(str));
}
}
9. ์ซ์๋ง ์ถ์ถ
import java.util.*;
public class Main {
public int solution(String s){
String answer="";
for(char x : s.toCharArray()){
if(Character.isDigit(x))
answer+=x;
}
return Integer.parseInt(answer);
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.nextLine();
System.out.print(T.solution(str));
}
}
10. ๋ฌธ์๊ฑฐ๋ฆฌ
import java.util.*;
public class Main {
public int[] solution(String s, char t){
int[] answer=new int[s.length()];
int p=1000;
for(int i=0; i<s.length(); i++){
if(s.charAt(i)==t){
p=0;
answer[i]=p;
}
else{
p++;
answer[i]=p;
} // ์ผ์ชฝ์ผ๋ก๋ถํฐ ๋จ์ด์ง ๊ฑฐ๋ฆฌ
}
p=1000;
for(int i=s.length()-1; i>=0; i--){
if(s.charAt(i)==t)
p=0;
else{
p++;
answer[i]=Math.min(answer[i],p); // ๊ธฐ์กด๊ฐ๊ณผ p๊ฐ์ค ๋ ์์๊ฒ
}
} // ์ค๋ฅธ์ชฝ๋ถํฐ ๋จ์ด์ง ๊ฑฐ๋ฆฌ
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str=kb.next();
char c=kb.next().charAt(0); // ๋ฌธ์ ํ๊ฐ ์ฝ์๋
for(int x : T.solution(str,c)){
System.out.print(x+" ");
}
}
}
11. ๋ฌธ์์ด ์์ถ
import java.util.*;
class Main {
public String solution(String s){
String answer="";
s=s+" ";
int cnt=1;
for(int i=0; i<s.length()-1; i++){
if(s.charAt(i)==s.charAt(i+1))
cnt++;
else {
answer += s.charAt(i);
if (cnt > 1)
answer += String.valueOf(cnt);
cnt = 1;
}
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
String str = kb.next();
System.out.print(T.solution(str));
}
}
12. ์ํธ ( replace(), parseInt(string, 2) )
import java.util.*;
class Main {
public String solution(int n,String s){
String answer="";
for(int i=0; i<n; i++){
String tmp=s.substring(0,7).replace('#','1').replace('*','0');
int num=Integer.parseInt(tmp,2);
answer+=(char)num;
s=s.substring(7);
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
String str=kb.next();
System.out.println(T.solution(n, str));
}
}
'๐ ์ฝ๋ฉํ ์คํธ > Inflearn' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์น์ 6. Sorting and Searching(์ ๋ ฌ, ์ด๋ถ๊ฒ์๊ณผ ๊ฒฐ์ ์๊ณ ๋ฆฌ์ฆ) (0) | 2022.12.07 |
---|---|
์น์ 5. Stack, Queue(์๋ฃ๊ตฌ์กฐ) (0) | 2022.10.16 |
์น์ 4. HashMap, TreeSet ( ํด์ฌ, ์ ๋ ฌ์ง์ Set ) (0) | 2022.09.28 |
์น์ 3. Two points, Sliding window[ํจ์จ์ฑ : O(n^2)-->O(n)] (0) | 2022.09.16 |
์น์ 2. Array( 1,2์ฐจ์ ๋ฐฐ์ด ) (0) | 2022.08.29 |