Interview Question
Senior Software Development EngineersCountry: United States
Interview Type: Written Test
Woody,
Thanks. But I have couple of questions:
1. if there is a word "1234" then looks like you will completely skip it, coz u r checking for isLetter in abvBg and abvEnd
2. the non alphabetic characters in string should stay as it is. I see you r skipping them all together.
public static void main(
String[] args) {
String sentence=args[0];
String[] arr = sentence.split(" ");
String abbr;
char first;
char last;
StringBuffer sb =new StringBuffer();
for (String word:arr) {
first=word.charAt(0);
last=word.charAt(word.length()-1);
abbr=first+String.valueOf(word.length()-2)+last;
sb.append(abbr).append(" ");
}
System.out.println(sb);
}
There are a few questions that need to be clarified:
1. If a word has numerals in it, since we need to leave it as is, what representation is needed for them. E.g. Global45 - what is the expected conversion?
2. What is expected if a word has number substring? E.g. Carl4Mayor.
3. If there is only a number, is that to be considered a word? E.g. 1234
Only when we understand these requirements, can we assess the solution (extra memory needed or not etc.)
import java.util.*;
public class Word {
private String sentence="mera naam deven hai";
private ArrayList<String> str= new ArrayList<String>();
public void tokens(String sentence)
{
char ch;
String temp="";
int len=0;
for(char c: sentence.toCharArray())
{
if(len==(sentence.length()-1))
{
temp+=c;
str.add(temp);
}
else if(c==' ')
{
str.add(temp);
temp="";
}
else
{
temp+=c;
}
len++;
}
//return str;
}
public void initials()
{
}
public static void main(String[] args)
{
Word w = new Word();
w.tokens(w.sentence);
char first,last;
int len;
String result="";
for(Object s: w.str.toArray())
{
String a=(String)s;
//System.out.println(a);
if(a.length()<=2)
{
System.out.print(a + " ");
}
else
{
len=a.length()-2;
result+=a.charAt(0);
result+=len;
result+=a.charAt(a.length()-1);
System.out.print(result+" ");
result="";
}
//System.out.println(s.toString());
}
}
}
public String processSentence(String sentence){
StringBuilder finalSentenceChars = new StringBuilder();
if(sentence == null){
return "";
}
char[] characters = sentence.toCharArray();
int position = 0;
ArrayList<Character> word = new ArrayList<Character>();
while (position != sentence.length()){
if(!Character.isAlphabetic(characters[position])){
if(!word.isEmpty()){
finalSentenceChars.append(transform(word));
word = new ArrayList<Character>();
}
finalSentenceChars.append(characters[position++]);
}else{
word.add(characters[position++]);
}
}
finalSentenceChars.append(transform(word));
return finalSentenceChars.toString();
}
private String transform(ArrayList<Character> word) {
return String.format("%c%d%c",word.get(0),(word.size() - 2), word.get(word.size() -1));
}
/**
* Programming Language used : PHP
* This method abbreviates the words in the $sentence.
* It would affect only words with alphabets only.
* e.g. "Human" will become "H4n". All non-alphabetic characters will be
* kept as it is.
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*
* @param string $sentence
* @return string abbreviated sentence
*/
function abbreviateSentence($sentence) {
$result = '';
$length = strlen($sentence);
$isWordStarted = false;
$wordInnerLength = 0;
for ($i = 0; $i < $length; $i++) {
if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
if ($isWordStarted) {
$wordInnerLength += 1;
} else {
$isWordStarted = true;
$result .= $sentence[$i];
}
} else {
if ($isWordStarted) {
$result .= (string) ($wordInnerLength - 1) . $sentence[$i - 1] . $sentence[$i];
$wordInnerLength = 0;
$isWordStarted = false;
} else {
$result .= $sentence[$i];
}
}
}
return $result;
}
/**
* Programming Language used : PHP
* This method abbreviates the words in the $sentence.
* It would affect only words with alphabets only.
* e.g. "Human" will become "H3n". All non-alphabetic characters will be
* kept as it is.
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*
* @param string $sentence
* @return string abbreviated sentence
*/
function abbreviateSentence($sentence)
{
$result = '';
$length = strlen($sentence);
$isWordStarted = false;
$wordInnerLength = 0;
for ($i = 0; $i < $length - 1; $i++) {
if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
if ($isWordStarted) {
$wordInnerLength += 1;
} else {
$isWordStarted = true;
$result .= $sentence[$i];
}
} else {
if ($isWordStarted) {
if ($wordInnerLength - 1 > 0) {
$result .= (string) ($wordInnerLength - 1);
}
$result .= $sentence[$i - 1] . $sentence[$i];
$wordInnerLength = 0;
$isWordStarted = false;
} else {
$result .= $sentence[$i];
}
}
}
if ($isWordStarted) {
if ($wordInnerLength > 0) {
$result .= (string) ($wordInnerLength);
}
}
$result .= $sentence[$i];
return $result;
}
/**
* Programming Language used : PHP
* This method abbreviates the words in the $sentence.
* It would affect only words with alphabets only.
* e.g. "Human" will become "H3n". All non-alphabetic characters will be
* kept as it is.
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*
* @param string $sentence
* @return string abbreviated sentence
*/
function abbreviateSentence($sentence)
{
$result = '';
$length = strlen($sentence);
$isWordStarted = false;
$wordInnerLength = 0;
for ($i = 0; $i < $length - 1; $i++) {
if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
if ($isWordStarted) {
$wordInnerLength += 1;
} else {
$isWordStarted = true;
$result .= $sentence[$i];
}
} else {
if ($isWordStarted) {
if ($wordInnerLength - 1 > 0) {
$result .= (string) ($wordInnerLength - 1);
}
$result .= $sentence[$i - 1] . $sentence[$i];
$wordInnerLength = 0;
$isWordStarted = false;
} else {
$result .= $sentence[$i];
}
}
}
//handled the boundary case separately, otherwise for every iteration it would
//be required to test whether its a boundary case or not.
if (preg_match('/^[A-Za-z]$/', $sentence[$length - 1])) {
if ($isWordStarted) {
if ($wordInnerLength > 0) {
$result .= (string) ($wordInnerLength);
}
}
} else {
if ($isWordStarted) {
if ($wordInnerLength - 1 > 0) {
$result .= (string) ($wordInnerLength - 1) . $sentence[$length - 2];
}
}
}
$result .= $sentence[$length - 1];
return $result;
}
public static String Abbreviate(String sentence){
char [] chs =sentence.toCharArray();
boolean prehasSpace=true;
int charCount = 0;
int j =0, i =0 ;
for ( ; i < chs.length ; ++i){
if (Character.isAlphabetic(chs[i])){
//find the start of the word
if (prehasSpace){
prehasSpace=false;
chs[j++]=chs[i];
}else{
charCount++;
}
}else{
prehasSpace=true;
if (Character.isAlphabetic(chs[i-1])){
chs[j++]=(char)((charCount-1)+'0');
chs[j++]=chs[i-1];
charCount=0;
}
chs[j++]=chs[i];
}
}
if (Character.isAlphabetic(chs[i-1])){
chs[j++]=(char)((charCount-1)+'0');
chs[j++]=chs[i-1];
}
return new String(chs,0,j);
}
{
- Woody November 22, 2013static void Main(string[] args)
{
string inString = "Listen my children and you shall hear of the midnite-ride of Paul Revere, 1 if by land and 2 if by sea, the redcoats are coming!";
string[] Words = inString.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
List<string> Outwords = new List<string>();
int BgnPos = 0;
int EndPos = Words.Length;
while (BgnPos < EndPos)
{
string AWord = Words[BgnPos];
if (AWord.Length < 3)
{
Outwords.Add(AWord);
}
else
{
string AbvBgn = string.Empty;
int l = AWord.Length;
bool hasFirst = false;
for (int i = 0; i < l; i++)
{
if (Char.IsLetter(AWord, i))
{
if (!hasFirst)
{
AbvBgn = AWord.Substring(0, i + 1).ToUpper();
hasFirst = true;
break;
}
}
}
string AbvEnd = string.Empty;
for (int i = l-1; i > 0; --i)
{
if (Char.IsLetterOrDigit(AWord, i))
{
AbvEnd = AWord.Substring(i, l - i).ToLower();
break;
}
}
int Cntr = 0;
string AbvMid = string.Empty;
for (int i = AbvBgn.Length; i < l - AbvEnd.Length; i++)
{
if (Char.IsLetterOrDigit(AWord, i))
{
Cntr++;
}
else
{
AbvMid = AbvMid + Cntr.ToString() + AWord[i];
Cntr = 0;
}
}
if (Cntr > 0)
{
AbvMid = AbvMid + Cntr.ToString();
}
string NewWord = AbvBgn + AbvMid + AbvEnd;
Outwords.Add(NewWord);
}
BgnPos++;
}
foreach (string Word in Outwords)
{
Console.WriteLine(Word);
}
string Oprkeys = Console.ReadKey().ToString();
}
}