์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ, ์ •๋ ฌ์ง€์› Set) : ์•„๋‚˜๊ทธ๋žจ

2023. 10. 22. 20:53ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1227

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ, ์ •๋ ฌ์ง€์› Set) : ํ•™๊ธ‰ ํšŒ์žฅ

https://hyejin.tistory.com/1226 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)] : ์ตœ๋Œ€ ๊ธธ์ด https://hyejin.tistory.com/1225 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, S

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

 

 

2. ์•„๋‚˜๊ทธ๋žจ 

์„ค๋ช…

Anagram์ด๋ž€ ๋‘ ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ์˜ ๋‚˜์—ด ์ˆœ์„œ๋ฅผ ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ตฌ์„ฑ์ด ์ผ์น˜ํ•˜๋ฉด ๋‘ ๋‹จ์–ด๋Š” ์•„๋‚˜๊ทธ๋žจ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด AbaAeCe ์™€ baeeACA ๋Š” ์•ŒํŒŒ๋ฒณ์„ ๋‚˜์—ด ์ˆœ์„œ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ตฌ์„ฑ์„ ์‚ดํŽด๋ณด๋ฉด A(2), a(1), b(1), C(1), e(2)๋กœ

์•ŒํŒŒ๋ฒณ๊ณผ ๊ทธ ๊ฐœ์ˆ˜๊ฐ€ ๋ชจ๋‘ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์–ด๋Š ํ•œ ๋‹จ์–ด๋ฅผ ์žฌ ๋ฐฐ์—ดํ•˜๋ฉด ์ƒ๋Œ€ํŽธ ๋‹จ์–ด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ์•„๋‚˜๊ทธ๋žจ์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋‘ ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋‘ ๋‹จ์–ด๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์•„๋‚˜๊ทธ๋žจ ํŒ๋ณ„์‹œ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.

 

์ž…๋ ฅ

์ฒซ ์ค„์— ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ๋˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์— ๋‘ ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.

๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

๋‘ ๋‹จ์–ด๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ด๋ฉด “YES"๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ˆ๋ฉด ”NO"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

AbaAeCe
baeeACA

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

YES

 

์˜ˆ์‹œ ์ž…๋ ฅ 2 

abaCC
Caaab

 

์˜ˆ์‹œ ์ถœ๋ ฅ 2

NO

 

 

๋ฌธ์ œ ํ’€์ด 1

public String solution(String a, String b) {
    String answer = "YES";
    Map<Character, Integer> mapA = new HashMap<>();
    Map<Character, Integer> mapB = new HashMap<>();
    for (char c : a.toCharArray()) {
        mapA.put(c, mapA.getOrDefault(c, 0) + 1);
    }

    for (char c : b.toCharArray()) {
        mapB.put(c, mapB.getOrDefault(c, 0) + 1);
    }

    for (Character x : mapA.keySet()) {
        if (mapA.get(x) != mapB.get(x)) {
            return "NO";
        }
    }

    return answer;
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป  : ๋‚˜๋Š” ๊ทธ๋ƒฅ ๊ฐ„๋‹จํ•˜๊ฒŒ HashMap 2๊ฐœ๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฌธ์ž์—ด a, b ๋ฅผ ๊ฐ๊ฐ Character ๋ฅผ Key ๋กœ ํ•˜๊ณ  ๊ทธ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์คฌ๋‹ค. 

๋‹ค์Œ mapA์˜ key ๊ฐ’์„ ๊ฐ€์ง€๊ณ  mapB์— key ๊ฐ’์„ get ํ•ด์„œ ๋‹ค๋ฅด๋ฉด NO ๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ํ–ˆ๋‹ค. 

(์‚ฌ์‹ค Key ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ ์ •๋‹ต ๋‚˜์™€์„œ ์ด๊ฒŒ ๋˜๋‚˜..? ์‹ถ๊ธด ํ–ˆ๋‹ค.) 

 

 

๋ฌธ์ œ ํ’€์ด 2

public String solution2(String s1, String s2) {
    String answer = "YES";
    HashMap<Character, Integer> map = new HashMap<>();
    for (char x : s1.toCharArray()) {
        map.put(x, map.getOrDefault(x, 0) + 1);
    }

    for (char x : s2.toCharArray()) {
        if (!map.containsKey(x) || map.get(x) == 0) {
            return "NO";
        }
        map.put(x, map.get(x) - 1);
    }


    return answer;
}

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜์€ ๋ฌธ์ œ๋ฅผ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€์—ˆ๋‹ค. ์šฐ์„  Map์€ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•ด์„œ s1 ๋ฌธ์ž์—ด์—์„œ Character๋ฅผ key๋กœ ์žก๊ณ  ๊ทธ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•˜๊ธด ํ–ˆ๋‹ค. (์—ฌ๊ธฐ๊นŒ์ง€๋Š” ๋™์ผ), ๊ทธ ๋‹ค์Œ์—” s2๋Š” Character๋กœ ๋ฌธ์ž ๋ฐฐ์—ด๋กœ ๋งŒ๋“ ๋‹ค์Œ, ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋Š”๋ฐ ์šฐ์„  map์— ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์žˆ๋Š”์ง€ ์ฆ‰, key ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ containsKey ๋ฉ”์„œ๋“œ๋กœ ํ™•์ธํ•ด์„œ ์—†๊ฑฐ๋‚˜ ํ˜น์€ map.get(x) ํ–ˆ๋Š”๋ฐ ์ด๋ฏธ ๋ฌธ์ž๋ฐฐ์—ด์„ ๊ฐœ์ˆ˜๊ฐ€ 0 ์ด๋ผ๋Š” ๊ฑด ๋ฌธ์ž ๊ฐœ์ˆ˜๊ฐ€ ๋งž์ง€ ์•Š๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ return NO๋ฅผ ํ•ด์คฌ๋‹ค. 

๋งŒ์•ฝ key ๊ฐ’์ด ์กด์žฌํ•œ๋‹ค๋ฉด map์—์„œ put๋ฅผ ํ•˜๋Š”๋ฐ ํ•ด๋‹น key ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€์„œ -1 ํ•ด์ค€๋‹ค. 

 

๊ตณ์ด Map์„ ๋‘๊ฐœ ์•ˆ๋งŒ๋“ค๊ณ  ํ’€ ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ..!! ๋‚˜๋Š” ๊ตณ์ด ๋‘๊ฐœ ๋งŒ๋“ค์–ด์„œ ๋น„๊ตํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.. 

 

 

 

 

 

 

 

 

728x90

'์ธํ”„๋Ÿฐ > ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ, ์ •๋ ฌ์ง€์› Set) : ๋ชจ๋“  ์•„๋‚˜๊ทธ๋žจ ์ฐพ๊ธฐ  (0) 2023.10.24
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ, ์ •๋ ฌ์ง€์› Set) : ๋งค์ถœ์•ก์˜ ์ข…๋ฅ˜  (0) 2023.10.23
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ, ์ •๋ ฌ์ง€์› Set) : ํ•™๊ธ‰ ํšŒ์žฅ  (1) 2023.10.21
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)] : ์ตœ๋Œ€ ๊ธธ์ด ์—ฐ์†๋ถ€๋ถ„์ˆ˜์—ด  (0) 2023.10.20
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)] : ์—ฐ์†๋œ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ 2 (์ˆ˜ํ•™์ ์œผ๋กœ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•)  (1) 2023.10.20