Problem Statement |
|||||||||||||
Typically, telephone numbers are sequences of digits (0-9) that all have the same length. However, some prefixes may be reserved for special purposes. This limits the total number of possible full-length telephone numbers that are available for general use in the system.
As an example, in much of the United States and Canada the local telephone numbers are 7 digits long. However, dialing 1 starts a special sequence for long distance, dialing 0 connects to the operator, and dialing 911 connects to emergency services. Thus, there are fewer than the theoretical 10,000,000 possible valid telephone numbers.
You are given the int digits: the length of the standard telephone numbers in the system. You are also given a String[] specialPrefixes. Each element of specialPrefixes is a string of digits that defines one reserved prefix. Standard telephone numbers cannot start with any of the reserved prefixes.
Compute and return the number of different standard telephone numbers in the system. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | digits will be between 1 and 7, inclusive. | ||||||||||||
- | specialPrefixes will contain beteween 0 and 50 elements, inclusive. | ||||||||||||
- | The length of each element of specialPrefixes will be between 1 and digits, inclusive. | ||||||||||||
- | Each character of each element of specialPrefixes will be a digit ('0'...'9'). | ||||||||||||
- | No element of specialPrefixes will itself be a prefix of another element. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
1 public class TCPhoneHomeEasy { 2 public static int validNumbers(int digits, String[] spcialPrefixes) { 3 int res=0; 4 int all=(int)Math.pow(10,digits); 5 for(int i=0;i<spcialPrefixes.length;i++) 6 { 7 int tmp=(int)Math.pow(10,digits-spcialPrefixes[i].length()); 8 res+=(all-tmp); 9 } 10 return res; 11 } 12 }