📌Back-End/프로그래머스

Java 프로그래머스 코딩 기초 트레이닝 Day 17 문자열

구 일 2024. 6. 13. 20:59
728x90
반응형

 

특정 문자열로 끝나는 가장 긴 부분 문자열 찾기

프로그래머스 특정문자열로 끝나는 가장 긴 부분 문자열 찾기 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 17
 * 특정 문자열로 끝나는 가장 긴 부분 문자열 찾기
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution("AbCdEFG", "dE"));
        System.out.println(solution("AAAAaaaa", "a"));
    }

    public static String solution(String myString, String pat) {
        int idx = myString.lastIndexOf(pat);
        return myString.substring(0, idx + pat.length());
    }
}

 

 

문자열이 몇 번 등장하는지 세기

프로그래머스 문자열이 몇 번 등장하는지 세기

 

 

/**
 * 코딩 기초 트레이닝 Day 17
 * 문자열이 몇 번 등장하는지 세기
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(solution("banana", "ana"));
        System.out.println(solution("aaaa", "aa"));
    }

    public static int solution(String myString, String pat) {
        int answer = 0;

        for (int i = 0; i < myString.length(); i++) {
            if (myString.startsWith(pat, i)) {
                answer++;
            }
        }

        return answer;
    }
}

 

 

ad 제거하기

프로그래머스 ad 제거하기 자바

 

 

import java.util.LinkedList;

/**
 * 코딩 기초 트레이닝 Day 17
 * ad 제거하기
 */
public class Main3 {
    public static void main(String[] args) {
        String[] result = solution(new String[]{"and", "notad", "abcd"});

        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();

        result = solution(new String[]{"there", "are", "no", "a", "ds"});

        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();
    }

    public static String[] solution(String[] strArr) {
        LinkedList<String> list = new LinkedList<>();

        for (int i = 0; i < strArr.length; i++) {
            if (!strArr[i].contains("ad")) {
                list.add(strArr[i]);
            }
        }

        return list.stream().toArray(String[]::new);
    }
}

 

공백으로 구분하기 1

프로그래머스 공백으로 구분하기 1 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 17
 * 공백으로 구분하기 1
 */
public class Main4 {
    public static void main(String[] args) {
        String[] result = solution("i love you");

        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();

        result = solution("programmers");

        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();
    }

    public static String[] solution(String my_string) {
        return my_string.split(" ");
    }
}

 

 

공백으로 구분하기 2

프로그래머스 공백으로 구분하기 2 자바

 

 

import java.util.LinkedList;

/**
 * 코딩 기초 트레이닝 Day 17
 * 공백으로 구분하기 2
 */
public class Main5 {
    public static void main(String[] args) {
        String[] result = solution("i  love you");

        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();

        result = solution(" programmers");

        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();
    }

    public static String[] solution(String my_string) {
        LinkedList<String> list = new LinkedList<>();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < my_string.length(); i++) {
            if (my_string.charAt(i) != ' ') {
                sb.append(my_string.charAt(i));
            } else if (sb.length() != 0) {
                list.add(sb.toString());
                sb = new StringBuilder();
            }
        }

        if (sb.length() != 0) {
            list.add(sb.toString());
        }

        return list.stream().toArray(String[]::new);
    }
}
728x90
반응형