본문 바로가기

Java/프로그래머스

[JAVA] 프로그래머스 - 가장 가까운 글자

https://school.programmers.co.kr/learn/courses/30/lessons/142086

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

시도)

import java.util.*;

class Solution {
    //배열에서 중복 확인
    public static boolean isDuplicate(char ch[], char c)
    {
        int count = 0;
        for(int i=0; i<ch.length; i++)
        {
            if(ch[i]==c)
            {
                count++;
            }
        }
    
        return (count>=2)?true:false;
    }

    //문자가 나온 가장 가까운 위치 반환 
    public static int position(char ch[], char c)
    {
        
        int startIdx = Arrays.asList(ch).indexOf(c);
        int idx = startIdx;
        
        for(int i=startIdx-1; i>=0; i--)
        {
            if(ch[i] ==c )
            {
                idx = i;
                break;
            }
        }
    
        return idx;
    }
    
    public int[] solution(String s) {
        int[] answer = new int[s.length()];
        char ch[] = s.toCharArray(); //[b,a,n,n,a]
        
        
        for(int i=0; i<s.length(); i++)
        {
            char cur = s.charAt(i); 
            
            if(!isDuplicate(ch,cur))
            {
                //중복되지 않는다면 
                answer[i] = -1;
            }
            else
            {
                //중복된다면
                //현재 인덱스 - cur이 나온 가장 가까운 인덱스 저장
                answer[i] = i-position(ch,cur); 
            }
        }
        return answer;
    }
}

 

 

정답 

 

import java.util.*;

class Solution {
    public int[] solution(String s) {
        int[] answer = new int[s.length()];
        
        //각 문자의 마지막 인덱스를 저장
        Map<Character,Integer> lastIndex = new HashMap<>();
        
        
        for(int i=0; i<s.length(); i++)
        {
            char cur = s.charAt(i);
            
            //현재 문자가 있다면
            if(lastIndex.containsKey(cur))
            {
                int lastIdx = lastIndex.get(cur); //현재 문자의 마지막 인덱스 가져옴 
                answer[i] = i-lastIdx;
            }
            else
            {
                answer[i] = -1;
            }
            
            lastIndex.put(cur,i); //문자, 현재 문자의 인덱스 map에 저장 
        }
        return answer;
    }
}

 

 

다른 사람의 풀이 

 

 

1)

import java.util.*;

class Solution {
    public int[] solution(String s) {
        int[] answer = new int[s.length()];
        HashMap<Character,Integer> map = new HashMap<>();
        for(int i=0; i<s.length();i++){
            char ch = s.charAt(i);
            answer[i] = i-map.getOrDefault(ch,i+1);
            map.put(ch,i);
        }
        return answer;
    }
}

 

 

2)

class Solution {
    public int[] solution(String str) {
        int[] result = new int[str.length()];

        for(int i=0;i<str.length();i++){

            String subStr = str.substring(0,i);
            if(subStr.indexOf(str.charAt(i))==-1) {
                result[i] = -1;
            }else {
                result[i] = i-subStr.lastIndexOf(str.charAt(i));
            }
        }
        return result;
    }
}