We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticList<String>vanity(List<String>codes,List<String>numbers){//avoid duplicated inputSet<String>uniqueNumbers=newHashSet<>(numbers);Set<String>uniqueCodes=newHashSet<>(codes);// generate the keypadMap<Character,Integer>t9Keymap=newHashMap<>();charcharacter='A'-1;for(inti=2;i<10;i++){for(intj=0;j<3;j++){t9Keymap.put(++character,i);}if(i==7||i==9){t9Keymap.put(++character,i);}}System.out.println(t9Keymap);//translate codes to "numbers"List<String>transformedCodes=newArrayList<>();uniqueCodes.forEach(code->{code=code.toUpperCase();StringBuildernumbersFromCodeBuilder=newStringBuilder();code.chars().forEach(c->numbersFromCodeBuilder.append(t9Keymap.get((char)c)));transformedCodes.add(numbersFromCodeBuilder.toString());});System.out.println(transformedCodes);//search the codes in the numbersListList<String>result=newArrayList<>();uniqueNumbers.forEach(number->transformedCodes.forEach(code->{if(number.contains(code)){result.add(number);}}));// sort and return the arrayresult.sort(String::compareTo);System.out.println(result);returnresult;}
Please Login in order to post a comment