Sandeep
BAN USERMy code also works for the case where input contains different numbers rather than repeating the same element and # and different number
567#6578#234
package com.rackspace.test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MobileImpl {
public String oldMobile(String input)
{
String[] inputArray = input.split("#"); // 444 55566688833 999666888
Map<Character,List<Character>> numPad = new HashMap<Character, List<Character>>();
numPad.put('2', Arrays.asList( 'A', 'B', 'C' ) );
numPad.put('3', Arrays.asList( 'D', 'E', 'F' ) );
numPad.put('4', Arrays.asList( 'G', 'H', 'I' ) );
numPad.put('5', Arrays.asList( 'J', 'K', 'L' ) );
numPad.put('6', Arrays.asList( 'M', 'N', 'O' ) );
numPad.put('7', Arrays.asList( 'P', 'Q', 'R', 'S' ) );
numPad.put('8', Arrays.asList( 'T', 'U', 'V' ) );
numPad.put('9', Arrays.asList( 'W','X', 'Y', 'Z' ) );
StringBuffer buffer = new StringBuffer();
for (String element : inputArray) {
char previous=element.charAt(0);
int count = 0;
int len = element.length()-1;
for(int i=0;i<element.length();i++)
{
char current = element.charAt(i);
if(current == previous)
{
count++;
if(i != len)
{
char next = element.charAt(i+1);
if(current != next)
{
System.out.println(buffer.toString());
List<Character> list = numPad.get(current);
char each = list.get(count-1);
buffer.append(each);
}
}else if(i == len)
{
buffer.append((numPad.get(current)).get(count-1));
}
}else
{
count = 1;
if(i != len)
{
char next = element.charAt(i+1);
if(current != next)
{
System.out.println(buffer.toString());
List<Character> list = numPad.get(current);
char each = list.get(count-1);
buffer.append(each);
}
}else if(i == len)
{
buffer.append((numPad.get(current)).get(count-1));
}
}
previous = current;
}
buffer.append(" ");
}
System.out.println(buffer.toString());
return buffer.toString();
}
}
My code also covers the case where you split the input and each element of the array contains different element.
package com.rackspace.test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MobileImpl {
public String oldMobile(String input)
{
String[] inputArray = input.split("#"); // 444 55566688833 999666888
Map<Character,List<Character>> numPad = new HashMap<Character, List<Character>>();
numPad.put('2', Arrays.asList( 'A', 'B', 'C' ) );
numPad.put('3', Arrays.asList( 'D', 'E', 'F' ) );
numPad.put('4', Arrays.asList( 'G', 'H', 'I' ) );
numPad.put('5', Arrays.asList( 'J', 'K', 'L' ) );
numPad.put('6', Arrays.asList( 'M', 'N', 'O' ) );
numPad.put('7', Arrays.asList( 'P', 'Q', 'R', 'S' ) );
numPad.put('8', Arrays.asList( 'T', 'U', 'V' ) );
numPad.put('9', Arrays.asList( 'W','X', 'Y', 'Z' ) );
StringBuffer buffer = new StringBuffer();
for (String element : inputArray) {
char previous=element.charAt(0);
int count = 0;
int len = element.length()-1;
for(int i=0;i<element.length();i++)
{
char current = element.charAt(i);
if(current == previous)
{
count++;
if(i != len)
{
char next = element.charAt(i+1);
if(current != next)
{
System.out.println(buffer.toString());
List<Character> list = numPad.get(current);
char each = list.get(count-1);
buffer.append(each);
}
}else if(i == len)
{
buffer.append((numPad.get(current)).get(count-1));
}
}else
{
count = 1;
if(i != len)
{
char next = element.charAt(i+1);
if(current != next)
{
System.out.println(buffer.toString());
List<Character> list = numPad.get(current);
char each = list.get(count-1);
buffer.append(each);
}
}else if(i == len)
{
buffer.append((numPad.get(current)).get(count-1));
}
}
previous = current;
}
buffer.append(" ");
}
System.out.println(buffer.toString());
return buffer.toString();
}
}
- Sandeep September 23, 2015