Vanity Number Search

  • + 0 comments
         
        static Map<Character, Integer> table;
        static Set<String> masterPhoneNums;
        public static List<String> vanity(List<String> codes, List<String> numbers) {
            table = initTable();        
            masterPhoneNums = new LinkedHashSet<>();
            masterPhoneNums.addAll(numbers);
            
            List<String> resPhone = new ArrayList<String>();
            
            String vanityCode;
            List subVanityNums;
            for(String code: codes){
                vanityCode = vanityCode2Num( code );
                subVanityNums = findVanityNum(vanityCode);
                if( subVanityNums.size()>0){
                    resPhone.addAll(subVanityNums);                
                }
                System.out.println("code:"+code+ " subVanityNums:"+subVanityNums);
                subVanityNums.clear();
            }
            Collections.sort(resPhone);
            return resPhone;
        }
    
        static List findVanityNum(String vanityNum){
            List arrPhNum = new ArrayList();
            System.out.println("total phone masterPhoneNums:"+masterPhoneNums.size());
            int counter=0;
            for(String phNum: masterPhoneNums) {
                if(phNum.contains(vanityNum)){
                    arrPhNum.add(phNum);
                    counter++;
                }
            }
            System.out.println("counter:"+counter);
            return arrPhNum;
        }
        
        static String vanityCode2Num(String vanityCode){
            String vanityNum = "";
            for(int i =0; i<vanityCode.length(); i++) {
                vanityNum += table.get(vanityCode.charAt(i));
            }
            System.out.println("vanityCode:"+vanityCode + " vanityNum:"+vanityNum);
            return vanityNum;
        }
        
        static Map<Character, Integer> initTable() {
           Map<Character, Integer> table = new HashMap<Character, Integer>(); 
            
            Character j='A';
            for(int i=2;i<10;i++,j++) {
                table.put(j,i);
                table.put(++j,i);
                table.put(++j,i);
                if(i==7 || i==9) {
                    table.put(++j,i);
                }
            }
            return table;
        }
    }