Problem Statement:
Create unique device names to be used in a residential IoT (Internet of Things) system. If a device name already exists in the system, an integer number is added at the end of the name to make it unique. The integer added starts with 1 and is incremented by 1 for each new request of an existing device name. Given a list of device name requests, process all requests and display an array of the corresponding unique device names.
Constraints- 1<=n<=10^4
- 1<=length of devicenames[i]<=20
- devicenames[i] contains only lowercase English letters in the range ASCII[a-z].
The first line contains an integer, n, denoting the size of the array.Output Format:
Each line i of the n subsequent lines (where 0<=i<n) contains a string.
Display unique device names in n lines.Sample Input:
6
switch
tv
switch
tv
switch
tv
Sample Output:
switch
tv
switch1
tv1
switch2
tv2
Solution in | C | JAVA | PYTHON |
---|
#include<stdio.h>
#include<string.h>
int main() {
char devicenames[10][20],result[10][20];
int n,i,j;
scanf("%d\n",&n);
for(i=0;i<n;i++){
fgets(devicenames[i],20,stdin);
}
for(i=0;i<n;i++){
devicenames[i][strlen(devicenames[i])-1]='\0';
puts(devicenames[i]);
}
for(i=0;i<n;i++){
int count=0;
char buf[3];
for(j=0;j<i;j++){
if(strcmp(devicenames[i],devicenames[j])==0){
count++;
}
}
if(count==0){
strcpy(result[i],devicenames[i]);
}else{
strcpy(result[i],devicenames[i]);
sprintf(buf,"%d",count);
strcat(result[i],buf);
}
}
for(i=0;i<n;i++){
puts(result[i]);
}
return 0;
}
#include<string.h>
int main() {
char devicenames[10][20],result[10][20];
int n,i,j;
scanf("%d\n",&n);
for(i=0;i<n;i++){
fgets(devicenames[i],20,stdin);
}
for(i=0;i<n;i++){
devicenames[i][strlen(devicenames[i])-1]='\0';
puts(devicenames[i]);
}
for(i=0;i<n;i++){
int count=0;
char buf[3];
for(j=0;j<i;j++){
if(strcmp(devicenames[i],devicenames[j])==0){
count++;
}
}
if(count==0){
strcpy(result[i],devicenames[i]);
}else{
strcpy(result[i],devicenames[i]);
sprintf(buf,"%d",count);
strcat(result[i],buf);
}
}
for(i=0;i<n;i++){
puts(result[i]);
}
return 0;
}
Input
6
switch
tv
switch
tv
switch
tv
Output
switch
tv
switch1
tv1
switch2
tv2
6
switch
tv
switch
tv
switch
tv
Output
switch
tv
switch1
tv1
switch2
tv2