https://leetcode.com/problems/partition-labels/submissions/ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
class Solution {
    public List<Integer> partitionLabels(String s) {
        //last Index 넣어라
        HashMap<String, Integer> hm = new HashMap<>();
        
        String[] splitMsg = s.split("");
        for(int i=0; i<splitMsg.length; i++){
            if(hm.containsKey(splitMsg[i])){
                hm.put(splitMsg[i], i+1);    
            }else{
               hm.put(splitMsg[i], i+1);    
            }
        }//end of for loop
        
        int lastIndex = 1;
        int lastValue = 0;
        List<Integer> answer = new LinkedList<>();
        
        //이미 확인된 알파벳은 더이상 보지 않습니다. 
        boolean[] visit = new boolean[123];
        for(int i=0; i<splitMsg.length; i++){
            
            if(hm.get(splitMsg[i])>lastIndex){
                // System.out.println("알파벳 : "+splitMsg[i]+" 몇번째 저장"+hm.get(splitMsg[i]) );
                //값을 빼서 저장
                lastIndex = hm.get(splitMsg[i]);
               
            }
            
            if(i+1 == lastIndex){
                // System.out.println("lastIndex : " + lastIndex);
                answer.add(lastIndex-lastValue);
                lastValue = lastIndex;
            }
           
        
        }
        return answer;
    }   
}
cs

'알고리즘' 카테고리의 다른 글

프로그래머스_실패율  (0) 2021.08.13
새로운게임_Java  (0) 2021.08.04
KAKAO_[3차]압축  (0) 2021.07.07
Kakao_보석쇼핑  (0) 2021.06.10
BOJ_14864_줄서기  (0) 2021.05.30