Skip to content

3491. Phone Number Prefix 👍

Approach 1: Sort

  • Time: $O(\texttt{sort} + n \cdot \max(|\texttt{numbers[i]}|))$
  • Space: $O(\texttt{sort})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  bool phonePrefix(vector<string>& numbers) {
    ranges::sort(numbers);
    for (int i = 1; i < numbers.size(); ++i)
      if (numbers[i].starts_with(numbers[i - 1]))
        return false;
    return true;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public boolean phonePrefix(String[] numbers) {
    Arrays.sort(numbers);
    for (int i = 1; i < numbers.length; ++i)
      if (numbers[i].startsWith(numbers[i - 1]))
        return false;
    return true;
  }
}
1
2
3
4
class Solution:
  def phonePrefix(self, numbers: list[str]) -> bool:
    return not any(b.startswith(a)
                   for a, b in itertools.pairwise(sorted(numbers)))

Approach 2: Trie

  • Time: $O(\Sigma |\texttt{numbers[i]}|)$
  • Space: $O(\Sigma |\texttt{numbers[i]}|)$
 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
48
49
50
51
52
struct TrieNode {
  vector<shared_ptr<TrieNode>> children;
  bool isWord = false;
  TrieNode() : children(10) {}
};

class Trie {
 public:
  void insert(const string& number) {
    shared_ptr<TrieNode> node = root;
    for (const char d : number) {
      const int i = d - '0';
      if (node->children[i] == nullptr)
        node->children[i] = make_shared<TrieNode>();
      node = node->children[i];
    }
    node->isWord = true;
  }

  // Returns true if the number is a prefix of any number in the trie or any
  // number in the trie is a prefix of the number.
  bool search(const string& number) {
    shared_ptr<TrieNode> node = root;
    for (const char d : number) {
      const int i = d - '0';
      if (node->children[i] == nullptr)
        return false;
      node = node->children[i];
      if (node->isWord)
        return true;
    }
    return true;  // The number is a prefix of some number in the trie
  }

 private:
  shared_ptr<TrieNode> root = make_shared<TrieNode>();
};

class Solution {
 public:
  bool phonePrefix(vector<string>& numbers) {
    Trie trie;

    for (const string& number : numbers) {
      if (trie.search(number))
        return false;
      trie.insert(number);
    }

    return true;
  }
};
 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
48
class TrieNode {
  public TrieNode[] children = new TrieNode[10];
  public boolean isWord = false;
}

class Trie {
  public void insert(String number) {
    TrieNode node = root;
    for (final char d : number.toCharArray()) {
      final int i = d - '0';
      if (node.children[i] == null)
        node.children[i] = new TrieNode();
      node = node.children[i];
    }
    node.isWord = true;
  }

  // Returns true if the number is a prefix of any number in the trie or any
  // number in the trie is a prefix of the number
  public boolean search(String number) {
    TrieNode node = root;
    for (final char d : number.toCharArray()) {
      final int i = d - '0';
      if (node.children[i] == null)
        return false;
      node = node.children[i];
      if (node.isWord)
        return true;
    }
    return true; // The number is a prefix of some number in the trie
  }

  private TrieNode root = new TrieNode();
}

class Solution {
  public boolean phonePrefix(String[] numbers) {
    Trie trie = new Trie();

    for (final String number : numbers) {
      if (trie.search(number))
        return false;
      trie.insert(number);
    }

    return true;
  }
}
 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 TrieNode:
  def __init__(self):
    self.children: dict[str, TrieNode] = {}
    self.isWord = False


class Trie:
  def __init__(self):
    self.root = TrieNode()

  def insert(self, number: str) -> None:
    node: TrieNode = self.root
    for d in number:
      node = node.children.setdefault(d, TrieNode())
    node.isWord = True

  def search(self, number: str) -> bool:
    """
    Returns True if the number is a prefix of any number in the trie or any
    number in the trie is a prefix of the number.
    """
    node: TrieNode = self.root
    for d in number:
      if d not in node.children:
        return False
      node = node.children[d]
      if node.isWord:
        return True
    return True  # The number is a prefix of some number in the trie.


class Solution:
  def phonePrefix(self, numbers: list[str]) -> bool:
    trie = Trie()

    for number in numbers:
      if trie.search(number):
        return False
      trie.insert(number)

    return True