항해99_코테스터디

[항해99]99클럽 코테 스터디 23일차 TIL + 문자열

아설아 2024. 6. 20. 00:40

 

 

1.  오늘의 학습 키워드 : 문자열

오늘 문제의 다른 사람들의 풀이를 보아하니 Hash Map객체로 푸는 풀이가 많더라고요. 그래서 관련 내용을 공부했습니다.

 

HashMap

데이터를 저장할 때 key, value가 짝을 이루어 저장되는 객체

데이터의 저장위치를 해시함수로 알 수 있기에 데이터의 추가, 삭제, 검색이 빠르다.

key값으로 해시함수를 실행한 결과를 통해 저장 위치를 결정. key값은 중복ㄴㄴ value는 키가 다르다면 가능

 

HashMap 생성방법


(방법1) HashMap<String, String> h1 = new HashMap<String, String>( );         // 기본 capacity:16, load factor:0.75 (방법2) HashMap<String, String> h2 = new HashMap<String, String>(20);       // capacity:20으로 설정
(방법3) HashMap<String, String> h3 = new HashMap<String, String>(20, 0.8); // capacity:20, load factor:0.8로 설정
(방법4) HashMap<String, String> h4 = new HashMap<String, String>(h1);      // 다른 Map(h1)의 데이터로 초기화
출처:
https://kadosholy.tistory.com/120
[KADOSHoly:티스토리]

2.  오늘의 문제  Decode the Message

https://leetcode.com/problems/decode-the-message/

 

3.  풀이 

class Solution {
    public String decodeMessage(String key, String message) {
        char[] hashes = new char[128];
        hashes[32] = 32;  //Ascii value of spaces is 32 and it will be unchainged
        char ch='a';
        for (char c : key.toCharArray()) {
            char hash = hashes[c];
            if (hash == 0) {
                hashes[c] = ch++;
            }
        }
        char[] result = new char[message.length()];
        for (int j = 0; j < message.length(); j++) {
            result[j] = hashes[message.charAt(j)];
        }
        return new String(result);
    }
}

 

3.  회고 

해쉬함수와 같이 기본이론이 많이 부족해서 더 공부를 해야겠습니다. 그래도 하루하루 문제를 풀며 성장하는 것을 느낍니다!

 

참고 링크

https://kadosholy.tistory.com/120