Interview Question
Developer Program EngineersCountry: India
Interview Type: Written Test
The question is : are we looking for unique words of 1 length? or not?
Assume we have the answer and we are taking care of this in every approach.
First Approach:
- Split by " " String[] token = input.split(" ") , go word by word and if it is of length 1 count++;
// it uses O(nr of words) additional space
SecondApproach:
- go element by element and if it is surround it by " " or if it is at the beginning of the string and followed by " " or it at the end of a string and preceded by " "
- if so count++;
// this one uses O(1) space
Third approach:
-use pattern matching:
Pattern p = Pattern.compile("\\w+");
Matcher m = p.matcher(input);
while(m.find()){
if(m.group().length()==1) count++;
}
assign each space char as operation +
each char as a value = 1
we need to convert the string into a numerical expression where a multiple char numerical value = 0
When a multiple characters string is found, its value would be str[0]-str[1], ignoring remaining characters in building expression
So the string "I am a king" will become
1+(1-1)+1+(1-1) = 2 //replace space with + and while scanning a multichar string, just scan first two chars
Complexity = O(n)
My Algorithm as following:
1) increase the count if the character is between A-Z or a-z (we can expand the list as needed).
2) for the rest characters, reset the counter.
3) Before reset the counter, we check and see if counter == 1
while (str != 0 && str[pos] != '\0')
{
if ((str[pos] >= 'A' && str[pos] <= 'Z') || (str[pos] >='a' && str[pos] <= 'z'))
{
counter++;
}
else
{
if (counter == 1)
{
printf("found:%c\n", str[pos-1]);
}
counter = 0;
}
pos++;
}
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int alpha(char *a)
{
int count=0;
if(*(a+1)==' ');
count++;
while(*a)
{
if(*(a-1)==' '&&*(a+1)=='\0')
count++;
if(*(a+1)==' '&&*(a-1)==' ')
count++;
a++;
}
return count;
}
int main()
{
char a[20];
gets(a);
cout<<alpha(a);
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
char arr[50]="i am a MAC ? a HOST";
int i=0,k=0,count=0;
while(i<strlen(arr))
{
if( arr[i]==' ')
{
printf("\n %d %c",i,arr[i-1]);
if( (65<=arr[i-1]&&arr[i-1]<=90) || ( 97<=arr[i-1] && arr[i-1]<=122 ) )
{
printf(" \t HI ");
printf("%d %d \n",i,k);
if((i-k)==1)
{
count+=1;
}
k=i+1;
}
else
{
k=i+1;
}
}
i++;
}
printf("\n Number of single letter words in a sentence:%d \n ",count);
return 0;
}
My implementation:
#include <stdio.h>
unsigned int countsingleword(char*p)
{
unsigned int count, count1;
if(NULL==p)
return 0;
for(count=0, count1=0;*p;p++)
{
if(' '==*p)
{
if(1==count1)
count++;
count1=0;
}else
{
count1++;
}
}
if(1==count1)
count++;
return count;
}
int main()
{
char p[]="I am a humble human! I am ";
printf("string %s has %d single word\n", p, countsingleword(p));
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int no_single_letter_words(char str[])
{
int length=0,check,count=0,i=0;
while(i<strlen(str))
{
if(str[i]!=' ')
length++;
else
{
check=length;
length=0;
if(check==1)
count++;
}
i++;
}
if(length==1)count++; //cheking if the last word consists of a single letter
return count;
}
int main()
{
char str[]="I am a King";
int answer=no_single_letter_words(str);
printf("%2d\n",answer);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char *p="I am a king";
int n=0,i;
for(i=0; i<strlen(p);i++)
{
if((i==1) || (i==(strlen(p)-2)))
if(p[i]==' ')
n++;
if((p[i]==' ') && (p[i+2]==' '))
n++;
}
printf(" Number of single letter word in the sentence is %d \n", n);
}
/* This is not a compact code ... Just an approach */
#include<stdio.h>
void wordcount(char *str)
{
char *temp = str;
int num =0,count = 0;
while(*temp!= '\0')
{
if(*temp == ' ' && num == 1)
{
count++;
num = 0;
}
else
{
num++;
}
if(*temp == ' ' )
{
num = 0;
}
temp++;
}
printf("the count is %d",count);
}
int main()
{
char *str = "I am a king";
wordcount(str);
return 0;
}
#include<stdio.h>
int main()
{
char c[] = "t t h to the j enfo j o k";
char *cp = c;
int count = 0;
if(c[1] == ' ')/*checks 1st character is single*/
count++;
while((*cp) != '\0') {
if((*cp) == ' ') {
if( *(cp+= 2) == ' ') {
count++;
cp-=2;/* i used to modify the pointer, so it missed some blanks*/
}
}
cp++;
}
if(c[strlen(c) - 2] == ' ')/*checks last character is single*/
count++;
printf("count = %d\n",count);
return 0;
}
#include <stdio.h>
int main() {
char *s = "I am A king";
int count = 0;
int base = 0;
int iter = 0;
while (*s != '\0') {
if (*s == ' ') {
if ((iter - base) == 1) {
count ++;
}
base = iter+1;
}
iter ++;
s ++ ;
}
printf("%d\n", count);
}
Let str[] denote the given string. I am creating an array arr[] which stores the words as I read them.
- alex December 14, 2012i=0,k=0,count=0
while(str[i++]!='\0')
do
if(str[i]!=' ')
arr[k++]=str[i]
count++
else
if(count==1)
arr is one of the ans and continue finding for others
k=0
empty arr again