Skip to content

Instantly share code, notes, and snippets.

@facundofarias
Created December 4, 2013 16:35
Show Gist options
  • Save facundofarias/7790737 to your computer and use it in GitHub Desktop.
Save facundofarias/7790737 to your computer and use it in GitHub Desktop.
PhoneList Coding Problem
/**
* Created with IntelliJ IDEA.
* User: ffarias
* Date: 11/29/13
* Time: 9:46 AM
* To change this template use File | Settings | File Templates.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PhoneList {
public static final String YES = "YES";
public static final String NO = "NO";
private static Tree tree = new Tree();
private static boolean isConsistent = true;
// Should be on a separate class
public static void main(String[] args) {
PhoneList pl = new PhoneList();
try {
pl.checkConsistency();
} catch (IOException e) {
e.printStackTrace();
}
}
public void checkConsistency() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; ++i) {
int n = Integer.parseInt(br.readLine());
tree = new Tree();
isConsistent = true;
for (int j = 0; j < n; ++j) {
String phone = br.readLine();
if (isConsistent)
buildTree(phone);
}
if (isConsistent)
System.out.println(YES);
else
System.out.println(NO);
}
}
private void buildTree(String s) {
int len = s.length();
Tree auxTree = tree;
for (int i = 0; i < len; ++i) {
int ch = Integer.parseInt(s.substring(i, i + 1));
Tree aux = auxTree.next[ch];
if (aux == null) {
aux = new Tree();
aux.node = 1;
if (i == len - 1)
aux.isCons = true;
auxTree.next[ch] = aux;
auxTree = aux;
} else {
if (aux.isCons) {
isConsistent = false;
break;
}
if (i == len - 1) {
isConsistent = false;
break;
}
auxTree = aux;
}
}
}
private static class Tree {
int node = 0;
boolean isCons = false;
Tree[] next = new Tree[10];
}
}
Phone list
Problem id: phonelist
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:
Emergency 911
Alice 97 625 999
Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1≤t≤40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1≤n≤10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
Sample inputSample output
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
NO
YES
Submitting
To submit your solution, send an email to puzzles@millicoder.com with your source files attached and the problem id as the subject. You will automatically get an email back with feedback on your solution. We accept Java, Python, C and C++ files. The input is read from stdin and the answer is read from stdout.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment