Bryant
BAN USERSolution in Java.
package com.practice;
import java.util.HashMap;
public class FormatedPhoneNumberTest {
private static HashMap<String, Integer> mapAlphaPhone = initPhoneMapping();
public static void main(String[] args) {
// Test
FormatedPhoneNumberTest fpnt = new FormatedPhoneNumberTest();
String phoneNumber = "1-800-COM-CAST";
System.out.println(fpnt.getFormattedPhoneNumber(phoneNumber));
}
public String getFormattedPhoneNumber(String phoneNumber) {
phoneNumber = removeSpecialChars(phoneNumber);
StringBuffer sf = new StringBuffer();
System.out.println("PHONE: " + phoneNumber);
for(int i=0; i < phoneNumber.length(); ++ i) {
if(mapAlphaPhone.containsKey(phoneNumber.substring(i, i + 1))) {
sf.append(mapAlphaPhone.get(phoneNumber.substring(i, i+1)));
}
else {
sf.append(phoneNumber.substring(i, i + 1));
}
}
return sf.toString();
}
public String removeSpecialChars(String str) {
str = str.replace("-", "");
str = str.replace(".", "");
str = str.replace("(", "");
str = str.replace(")", "");
return str;
}
public static HashMap<String, Integer> initPhoneMapping() {
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("A",2);
hashMap.put("B",2);
hashMap.put("C",2);
hashMap.put("D",3);
hashMap.put("E",3);
hashMap.put("F",3);
hashMap.put("G",4);
hashMap.put("H",4);
hashMap.put("I",4);
hashMap.put("J",5);
hashMap.put("K",5);
hashMap.put("L",5);
hashMap.put("M",6);
hashMap.put("N",6);
hashMap.put("O",6);
hashMap.put("P",7);
hashMap.put("Q",7);
hashMap.put("R",7);
hashMap.put("S",7);
hashMap.put("T",8);
hashMap.put("U",8);
hashMap.put("V",8);
hashMap.put("W",9);
hashMap.put("X",9);
hashMap.put("Y",9);
hashMap.put("Z",9);
return hashMap;
}
}
Solution in Java.
package com.practice;
public class NonRepeatingCharTest {
public static void main(String[] args) {
NonRepeatingCharTest nct = new NonRepeatingCharTest();
// First non repeating character in the list in this example is c
// though 2, 4, d also qualifies for non repeating characters
char[] list = new char[] {'a','b','c','2','4','b','a','d'};
int answer = nct.getFirstNonRepeatingChar(list);
if(answer == -1) {
System.out.println("No non repeating character in the list");
}
else {
System.out.println("First non repeating character in the list is " + (char)answer);
}
}
public int getFirstNonRepeatingChar(char[] list) {
int[] listAscii = new int[256];
// Count the number of character for each asccii character
for(int i=0; i < list.length; ++ i) {
listAscii[list[i]] ++;
}
for(int i=0; i < list.length; ++ i) {
if(listAscii[list[i]] == 1)
return list[i];
}
return -1;
}
}
There are at least two ways to implement this: one with linear probing and another one with separate chaining. Here is the version for separate chaining in Java. This also assumes that the key is actually the integers. It will break if the keys are not in integers.
package com.practice.hashtable.separatechaining;
public class SeparateChainingExample {
public static void main(String[] args) {
SeparateChainingExample sce = new SeparateChainingExample();
// Test Hash Table
LinkedHashTable lht = sce.new LinkedHashTable();
lht.put(0, "A");
lht.put(1, "B");
lht.put(2, "C");
lht.put(3, "D");
lht.put(4, "E");
lht.put(5, "F");
lht.put(6, "G");
lht.put(7, "H");
lht.put(13,"I");
lht.put(21, "J");
sce.testPrint(lht.hashTable);
}
public void testPrint(LinkedHashEntry[] list) {
int counter = 0;
for(LinkedHashEntry lhe : list) {
System.out.println("NODE #" + counter + " SIZE: " + lhe.getSize());
LinkedHashEntry entry = lhe;
while((entry != null)) {
System.out.println("\t" + entry.getKey() + " , " + entry.getValue());
entry = entry.getNext();
}
++ counter;
}
}
class LinkedHashTable {
private final static int TABLE_SIZE = 5;
private LinkedHashEntry[] hashTable;
public LinkedHashTable() {
this.hashTable = new LinkedHashEntry[TABLE_SIZE];
for(int i=0; i < TABLE_SIZE; ++ i) {
hashTable[i] = null;
}
}
public void put(Object key, Object value) {
int hashValue = ((Integer)(key) % TABLE_SIZE);
if(hashTable[hashValue] == null) {
hashTable[hashValue] = new LinkedHashEntry(key, value);
}
else {
LinkedHashEntry entry = hashTable[hashValue];
while((entry.getNext() != null) && (!entry.getKey().equals(key))) {
entry = entry.getNext();
}
if(entry.getKey().equals(key)) {
entry.setValue(value);
}
else {
entry.setNext(new LinkedHashEntry(key, value));
}
}
}
public Object get(Object key) {
int hashValue = ((Integer)(key) % TABLE_SIZE);
if(hashTable[hashValue] == null) {
return null;
}
else {
LinkedHashEntry entry = hashTable[hashValue];
while((entry != null) && (!entry.getKey().equals(key))) {
entry = entry.getNext();
}
if(entry == null) {
return null;
}
else {
return entry.getValue();
}
}
}
}
class LinkedHashEntry {
private Object key;
private Object value;
private LinkedHashEntry next;
private int size;
/**
* Default Constructor
*/
public LinkedHashEntry(Object key, Object value) {
this.key = key;
this.value = value;
this.next = null;
this.size = 1;
}
public void setKey(Object key) {
this.key = key;
}
public Object getKey() {
return key;
}
public void setValue(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
public void setNext(LinkedHashEntry next) {
this.next = next;
++ size;
}
public LinkedHashEntry getNext() {
return next;
}
public int getSize() {
return size;
}
}
}
- Bryant March 01, 2014