leetcode 알고리즘 문제풀이 210612

릿코드 알고리즘 문제풀이

1번

2번

3번

4번





1번

import static java.lang.System.*;

class Solution {
    public boolean halvesAreAlike(String s) {
        int front = 0;
        int back = 0;
        for(int i=0 ; i<=s.length()/2 ; i++) {
            if(isInclude(s.charAt(i))) {
                front++;
            }

            if(isInclude(s.charAt((s.length()/2)+i+1))) {
                back++;
            }

            //
        }
        //
        //

        return front==back;
    }

    private boolean isInclude(char c) {
        char[] map = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        for(char t : map) {
            if(t==c) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println( s.halvesAreAlike("Book"));
    }
}





2번

import java.util.HashSet;
import java.util.Set;

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        Set<String> mt = new HashSet<>();

        for (String s : words) {
            String convedMsg = wordToMorse(s);
            mt.add(convedMsg);
        }


        return mt.size();
    }

    private String wordToMorse(String s) {
        String[] MORSE = {".-", "-...", "-.-.",
        "-..", ".", "..-.", "--.", "....", 
        "..", ".---", "-.-", ".-..", "--", 
        "-.", "---", ".--.", "--.-", ".-.", 
        "...", "-", "..-", "...-", ".--", 
        "-..-", "-.--", "--.."};
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            builder.append(MORSE[(s.charAt(i) - 'a')]);
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.uniqueMorseRepresentations(new String[]{"gin", "zen", "gig", "msg"}));
    }
}





3번

import java.util.Arrays;

class Solution {

    public boolean canMakeArithmeticProgression(int[] arr) {
        Arrays.sort(arr);

        int dif = arr[1] - arr[0];

        for (int i = 0; i < arr.length - 1; i++) {
            if ((arr[i + 1] - arr[i]) == dif) {
                continue;
            } else {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.canMakeArithmeticProgression(new int[]{1, 7, 5}));
        System.out.println(s.canMakeArithmeticProgression(new int[]{0, 0, 0, 0}));
        System.out.println(s.canMakeArithmeticProgression(new int[]{1, 2, 4}));
        System.out.println(s.canMakeArithmeticProgression(new int[]{3, 5, 1}));
    }

}





4번

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

class Solution {
    public int minDeletionSize(String[] strs) {
        int count = 0;
        
        for(int i = 0;i < strs[0].length();i++){ // 문자열의 길이는 같다 
            for(int j = 0;j < strs.length - 1;j++){
                if(strs[j].charAt(i) > strs[j+1].charAt(i)){
                    count++;
                    break;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.minDeletionSize(new String[]{"gin", "zen", "gig", "msg"}));
    }

}

좋은 웹페이지 즐겨찾기