'개발/CodingTest'에 해당되는 글 10건

  1. 2020.06.02 HackerRank - Anagrams
  2. 2020.06.01 HackerRank - Counting Valley

 

 

static boolean isAnagram(String a, String b) {
        // Complete the function
        //대소문자 구분안함
        a = a.toUpperCase();
        b = b.toUpperCase();
        int alen = a.length();
        int blen = b.length();
        //길이가 다르면 아나그램이 아니다
        if(alen != blen){
            return false;
        }
        
        char[] arrA = a.toCharArray();
        //a문자열에 있는 문자를 하나씩 탐색하면서 b문자열에서 제거한다.
        for(char ch : arrA){            
            b = b.replaceFirst(Character.toString(ch), "");
            //System.out.println("ch="+ch+",b="+b);
        }
        
        //b문자열이 전부 제거되었으면 아나그램 아니면 아나그램 아님
        if(b.isEmpty()) return true;
        else return false;
    }

'개발 > CodingTest' 카테고리의 다른 글

Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
프로그래머스 - 기능개발  (0) 2020.06.03
프로그래머스 - 탑  (0) 2020.06.03
HackerRank - Counting Valley  (0) 2020.06.01
Posted by Lumasca
,

 

// Complete the countingValleys function below.
    static int countingValleys(int n, String s) {
        int seaLevel = 0;//높이
        int valley = 0;
        boolean isValley = false;
        for(int i = 0; i< n;i++){
            char ch = s.charAt(i);
            if(ch == 'U'){//올라가면 높이증가
                seaLevel++;
            }else if(ch == 'D'){//내려가면 높이 감소
                seaLevel--;
            }
            if(seaLevel == 0){//높이가 0에 도달하면 
                if(isValley){//valley인 경우만 카운팅
                    valley++;    
                }            
            }else if(seaLevel > 0){//높이가 0보다 크면 valley가 아님
                isValley = false;
            }else if(seaLevel < 0){//높이가 0보다 작으면 valley
                isValley = true;
            }
        }
        return valley;
    }

 

'개발 > CodingTest' 카테고리의 다른 글

Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
프로그래머스 - 기능개발  (0) 2020.06.03
프로그래머스 - 탑  (0) 2020.06.03
HackerRank - Anagrams  (0) 2020.06.02
Posted by Lumasca
,