알고리즘 정리/[baek]
[baek] 공부 기록(java)
로미로미로
2024. 5. 25. 20:39
10988. 팰린드롬인지 확인하기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String S = scan.next();
boolean isTrue = true;
for(int i=0; i<S.length()/2; i++) {
if(S.charAt(i) != S.charAt(S.length()-i-1)) {
isTrue = false;
}
}
if(isTrue == true)
System.out.println(1);
else
System.out.println(0);
}
}
1157. 단어 공부
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String S = scan.next();
int arr[] = new int[26];
int max = -1;
char c = '?';
int maxIndex = 0;
for(int i=0; i<S.length(); i++) {
if(97 <= S.charAt(i) && S.charAt(i)<=122) {
int num = S.charAt(i) - 32;
arr[num-65] ++;
}
else
arr[S.charAt(i)-65]++;
}
for(int i=0; i<26; i++) {
if(arr[i] > max ) {
max = arr[i];
c = (char) (i+65);
}
else if (arr[i] == max)
c = '?';
}
System.out.println(c);
}
}
-> ?를 출력하는 것이 조금 까다로웠음.
1316.그룹 단어 체커
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int num = 0;
for(int i = 0; i<N; i++) {
boolean isTrue = false;
String S = scan.next();
for(int j=0; j<S.length()-2; j++) {
if(S.charAt(j) != S.charAt(j+1)) {
for(int k=j+2; k<S.length(); k++) {
if(S.charAt(j) == S.charAt(k)) {
isTrue = true;
}
}
}
}
if(isTrue == false) {
num++;
}
}
System.out.println(num);
}
}
-> 이 문제는 그냥 연속된 알파벳이 아니면 그 뒤에서부터 해당 알파벳이 찾아지냐만 따짐.