Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

์„ค๋ช…

๋ฌธ์ž์—ด ๋ฐฐ์—ด๊ณผ prefix ๋‹จ์–ด, suffix ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์กŒ์„๋•Œ, ํ•ด๋‹น prefix ์™€ suffix ๋ฅผ ๋™์‹œ์— ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋‹จ์–ด ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผํ•œ๋‹ค. ๋งŒ์•ฝ ์œ ํšจํ•œ ์ธ๋ฑ์Šค๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ด๋ฉด ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

ํ’€์ด (HashMap)

๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ ‘๋‘์‚ฌ, ์ ‘๋ฏธ์‚ฌ ์กฐํ•ฉ์„ HashMap ์˜ key ๋กœ ๋‹ด์•„๋‘”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์—์„œ ๋ช‡๋ฒˆ์งธ ๋‹จ์–ด์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์ธ๋ฑ์Šค๋ฅผ value ๋กœ ๋‹ด์•„๋‘”๋‹ค.

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
String[] words = {"apple"};

// hashMap
{
  "a#e": 0,
  "a#le": 0,
  "a#ple": 0,
  "a#pple": 0,
  "a#apple": 0,
  "ap#e": 0,
  "ap#le": 0,
  "ap#ple": 0,
  "ap#pple": 0,
  "ap#apple": 0,
  "app#e": 0,
  "app#le": 0,
  "app#ple": 0,
  "app#pple": 0,
  "app#apple": 0,
  "appl#e": 0,
  "appl#le": 0,
  "appl#ple": 0,
  "appl#pple": 0,
  "appl#apple": 0,
  "apple#e": 0,
  "apple#le": 0,
  "apple#ple": 0,
  "apple#pple": 0,
  "apple#apple": 0
}
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
class WordFilter {
    private Map<String, Integer> map = new HashMap<>();
    
    public WordFilter(String[] words) {
        for (int i = 0; i < words.length; i++) {
          String word = words[i];
          int length = word.length();
          for (int j = 1; j <= length; j++) {
            for (int k = 1; k <= length; k++) {
              String key = word.substring(0, j) + "{" + word.substring(length - k);
              map.put(key, i);
            }
          }
        }
    }
    
    public int f(String pref, String suff) {
        String s = pref + "{" + suff;
        return map.getOrDefault(s, -1);
    }
}

/**
 * Your WordFilter object will be instantiated and called as such:
 * WordFilter obj = new WordFilter(words);
 * int param_1 = obj.f(pref,suff);
 */

์‹œ๊ฐ„๋ณต์žก๋„

  1. ์ƒ์„ฑ์ž(WordFilter) ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ L ์ด๋ฉด ํ•œ ๋‹จ์–ด์— ๋Œ€ํ•ด ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ ์ˆ˜๋Š” ์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๊ฐ€ ๊ฐ๊ฐ L ๊ฐœ ์ด๋ฏ€๋กœ L^2 ์ด ๋œ๋‹ค. N ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์„๋•Œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N * L^2) ์ด ๋œ๋‹ค.

  2. ์ฟผ๋ฆฌ ๋ฉ”์†Œ๋“œ(f) ํ•ด์‹œ๋งต ํ‚ค์— ์ •ํ™•ํžˆ ๋งž๋Š” ๊ฐ’์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ ๋•Œ๋ฌธ์— O(1) ์ด๋‹ค.

ํ’€์ด (Trie)

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
42
43
44
45
46
47
class TrieNode {
  Map<Character, TrieNode> children;
  int index;

  public TrieNode() {
    children = new HashMap<>();
    index = -1;
  }
}

class WordFilter {
  private TrieNode root;

  public WordFilter(String[] words) {
    root = new TrieNode();
    int n = words.length;
    for (int i = 0; i < n; i++) {
      String word = words[i];
      int len = word.length();
      // ๋ชจ๋“  prefix์™€ suffix ์กฐํ•ฉ์„ ์ €์žฅ
      for (int j = 0; j <= len; j++) {
        String modifiedWord = word.substring(j) + "{" + word;
        insert(modifiedWord, i);
      }
    }
  }

  private void insert(String word, int index) {
    TrieNode current = root;
    for (char ch : word.toCharArray()) {
      current = current.children.computeIfAbsent(ch, c -> new TrieNode());
      current.index = index; // ํ˜„์žฌ ๋…ธ๋“œ์— ๊ฐ€์žฅ ํฐ ์ธ๋ฑ์Šค ์ €์žฅ
    }
  }

  public int f(String prefix, String suffix) {
    String searchWord = suffix + "{" + prefix;
    TrieNode current = root;
    for (char ch : searchWord.toCharArray()) {
      current = current.children.get(ch);
      if (current == null) {
        return -1;
      }
    }
    return current.index;
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ