제육's 휘발성 코딩
Published 2021. 8. 26. 14:15
CH1. 문자열 🔷 Java/Algorithm
반응형

문자 찾기

1-1

public class Main {
    public int solution(String str, char t) {
        int answer = 0;
        str = str.toUpperCase();
        t = Character.toUpperCase(t);

        for (char x : str.toCharArray()) {
            if ( x == 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));
    }
}

대소문자 변환

1-2

import java.util.Scanner;

public class Section1_2 {

    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) {
        Section1_2 main = new Section1_2();
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        System.out.print(main.solution(name));
    }
}

아스키코드 : 65<=x && x<=90 : 대문자, 97<= x<=122 소문자

x-32 , x+32 와 같이 표시하면 자동으로 정수형으로 변환되어 아스키코드로 계산된다.

문장 속 단어

1-3

import java.util.Scanner;

public class Section1_3 {

    public String solution(String str) {
        String answer = "";
        int m = Integer.MIN_VALUE;  // 가장 작은 정수 값
        String[] s = str.split(" "); // 공백 구분
        for (String x : s) {
            int len = x.length();
            if (len > m) {
                m = len;
                answer = x;
            }
        }

        return answer;
    }

    public String solution2(String str) {
        String answer = "";
        int m = 0;
        int pos ;
        while ((pos = str.indexOf(' ')) != -1) { //공백을 만났을 때
            String tmp = str.substring(0, pos);
            int len = tmp.length();
            if (len > m) {
                m = len;
                answer = tmp;
            }
            str = str.substring(pos + 1);
        }
        if (str.length() > m) {  //마지막 단어
            answer = str;
        }
        return answer;
    }

    public static void main(String[] args) {
        Section1_3 main = new Section1_3();
        Scanner kb = new Scanner(System.in);
        String str = kb.nextLine();
        System.out.print(main.solution2(str));
    }
}

단어 뒤집기

1-4

import java.util.*;

public class Section1_4 {

    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 ArrayList<String> solution2(int n, String[] str) {
        ArrayList<String> answer = new ArrayList<>();
        for (String x : str) {
            char[] s = x.toCharArray(); //char 배열로 변경
            int lt = 0, rt = x.length() - 1;
            while (lt < rt) {
                char tmp = s[lt];
                s[lt] = s[rt];
                s[rt] = tmp;
                lt++;
                rt--;
            }
            String tmp = String.valueOf(s);
            answer.add(tmp);
        }

        return answer;
    }

    public static void main(String[] args) {
        Section1_4 main = new Section1_4();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = sc.next();
        }
        for (String x : main.solution2(n, str)) {
            System.out.println(x);
        }
    }
}

특정 문자 뒤집기

1-5

import java.util.Scanner;

public class Section1_5 {

    public String solution(String str) {
        String answer;
        char[] s = str.toCharArray();
        int lt = 0;
        int rt = s.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);
        return answer;
    }

    public String solution2(String str) {
        String answer="";
        char[] x = str.toCharArray();
        int lt = 0;
        int rt = x.length-1;

        while (lt < rt) {

            if (!(65<=x[lt] && x[lt]<=90) && !(97<=x[lt] && x[lt]<=122)){
                lt++;
            } else if (!(65<=x[rt] && x[rt]<=90) && !(97<=x[rt] && x[rt]<=122)) {
                rt--;
            }
            else {
                char tmp = x[lt];
                x[lt] = x[rt];
                x[rt] = tmp;
                lt++;
                rt--;
            }
        }
        answer = String.valueOf(x);

        return answer;
    }

    public static void main(String[] args) {
        Section1_5 main = new Section1_5();
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        System.out.println(main.solution2(str));
    }
}

중복 문자 제거

1-6

import java.util.Scanner;

public class Section1_6 {

    public String solution(String str) {

        String answer = "";
        for (int i = 0; i < str.length(); i++) {
            // indexOf : 앞에서부터 처음 발견되는 인덱스를 반환, 없을경우 -1 반환
            //System.out.println(str.charAt(i) + " " + i + " " + str.indexOf(str.charAt(i)));
            if (str.indexOf(str.charAt(i)) == i) {
                answer += str.charAt(i);
            }
        }

        return answer;


    }

    public static void main(String[] args) {
        Section1_6 main = new Section1_6();
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        System.out.println(main.solution(str));
    }
}

회문 문자열

1-7

import java.util.Scanner;

public class Section1_7 {

    public String solution(String str) {
        str = str.toUpperCase();
        char[] x = str.toCharArray();
        int lt = 0;
        int rt = x.length-1;

        while (lt < rt) {
            char tmp = x[lt];
            x[lt] = x[rt];
            x[rt] = tmp;
            lt++;
            rt--;
        }
        String answer = String.valueOf(x);
        if (answer.equals(str)){
            return "YES";
        }
        else {
            return "NO";
        }
    }

    public String solution2(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 String solution3(String str) {
        String answer = "YES";
        str = str.toUpperCase();
        String tmp = new StringBuilder(str).reverse().toString();
//        if (!tmp.equals(str))  answer = "NO";
        if (!str.equalsIgnoreCase(tmp)) answer="NO"; //equals() : 대소문자 구분, equalsIgnoreCase() : 대소문자 비교 x
        return answer;
    }

    public static void main(String[] args) {
        Section1_7 main = new Section1_7();
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        System.out.println(main.solution3(str));
    }
}

유효한 팰린드롬

1-8

import java.util.Scanner;

public class Section1_8 {

    public String solution(String str) {
        String answer = "NO";
        // 정규 표현식
        str = str.toUpperCase().replaceAll("[^A-Z]", " ");
        String tmp = new StringBuilder(str).reverse().toString();
        if(str.equals(tmp)){
            answer = "YES";
        }
        return answer;
    }

    public static void main(String[] args) {
        Section1_8 main = new Section1_8();
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        System.out.println(main.solution(str));
    }
}

숫자만 추출

1-9

import java.util.Scanner;

public class Section1_9 {

    public int solution(String str) {
        str = str.replaceAll("[^0-9]", "");
        int a = Integer.parseInt(str);
        return a;
    }

    public int solution2(String str) {
        int answer = 0;
        for (char x : str.toCharArray()) {
            if (48 <= x && x <= 57) { //알파벳인경우 (48 :0 , 57:9)
                answer = answer * 10 + (x-48);
            }
        }

        return answer;
    }

    public int solution3(String str) {
        String answer = "";
        for (char x : str.toCharArray()) {
            if (Character.isDigit(x)) { // 숫자면
                answer += x;
            }
        }

        return Integer.parseInt(answer);
    }
    public static void main(String[] args) {
        Section1_9 main = new Section1_9();
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        System.out.println(main.solution3(str));
    }
}

가장 짧은 문자 거리

1-10

import java.util.Scanner;

public class Section1_10 {
    public int[] solution(String str1, char str2) {
        int[] answer = new int[str1.length()];
        int p = 1000;
        for (int i = 0; i < str1.length(); i++) {
            if (str1.charAt(i) == str2) {
                p = 0;
            }
            else {
                p++;
            }
            answer[i] = p;
        }
        p = 1000;
        for (int i = str1.length() - 1; i > -1; i--) {
            if (str1.charAt(i) == str2) {
                p = 0;
            }
            else {
                p++;
                answer[i] = Math.min(answer[i], p);
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        Section1_10 main = new Section1_10();
        Scanner sc = new Scanner(System.in);
        String str1 = sc.next();
        char str2 = sc.next().charAt(0);
        for (int x : main.solution(str1, str2)) {
            System.out.print(x+" ");
        }

    }
}

문자열 압축

1-11

import java.util.Scanner;

public class Section1_11 {

    public String solution(String str) {
        int count = 1;
        String answer = "";
        char index = ' ';
        for (int i = 0; i < str.length(); i++) {
            if (index != str.charAt(i)) {
                if (count > 1) {
                    answer += count;
                }
                count = 1;
                index = str.charAt(i);
                answer += index;
            } else {
                count++;
                if (i == str.length()-1 && count > 1) {
                    answer +=count;
                }
            }
        }
        return answer;
    }

    public String solution2(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) {
        Section1_11 main = new Section1_11();
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        System.out.println(main.solution2(str));
    }
}

암호

1-12

import java.util.Scanner;

public class Section1_12 {

    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'); // 0부터 7전 까지
            int num = Integer.parseInt(tmp, 2);
            answer += (char)num; // 문자로 형변환
            s = s.substring(7);
            System.out.println(s);
        }

        return answer;
    }

    public static void main(String[] args) {
        Section1_12 main = new Section1_12();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String str = sc.next();
        System.out.println(main.solution(n, str));
    }
}
반응형
profile

제육's 휘발성 코딩

@sasca37

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요! 맞구독은 언제나 환영입니다^^