-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandidateCode.java
More file actions
125 lines (84 loc) · 2.53 KB
/
CandidateCode.java
File metadata and controls
125 lines (84 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
PROBLEM STATEMENT
Virus Outbreak (100 Marks)
In the Martian land faraway, a new virus has evolved and is attacking the individuals at a fast pace. The scientists have figured out the virus composition, V. The big task is to identify the people who are infected. The sample of N people is taken to check if they are POSITIVE or NEGATIVE. A report is generated which provides the current blood composition B of the person.
POSITIVE or NEGATIVE ?
If the blood composition of the person is a subsequence of the virus composition V, then the person is identified as POSITIVE otherwise NEGATIVE.
Example:
Virus Composition, V = coronavirus
Blood Composition of the person , B = ravus
The person in question is POSITIVE as B is the subsequence of the V.
The scientists are busy with their research for medicine and request you to build a program which can quickly figure out if the person is POSITIVE or NEGATIVE. They will provide you with the virus composition V and all the people’s current blood composition. Can you help them?
Note: The virus and blood compositions are lowercase alphabet strings.
Constraints
1<= N <=10
1<= |B|<= |V|<= 10^5
Input Format
The first line of the input consists of the virus composition, V
The second line of he input consists of the number of people, N
Next N lines each consist of the blood composition of the ith person, Bi
Output Format
For each person, print POSITIVE or NEGATIVE in a separate line
Sample TestCase 1
Input
coronavirus
3
abcde
crnas
onarous
Output
NEGATIVE
POSITIVE
NEGATIVE
*/
// VIRUS OUTBREAK CODE
import java.util.Scanner;
public class CandidateCode
{
public void subsequence(String str, String virusComp)
{
int k = 0,count = 0;
for(int i = 0; i < str.length(); i++)
{
for(int j = k; j < virusComp.length(); j++)
{
if(str.charAt(i) == virusComp.charAt(j))
{
count++;
k = j++;
break;
}
}
}
if (count == str.length())
{
System.out.println("POSITIVE");
}
else
{
System.out.println("NEGATIVE");
}
}
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
String virusComp = sc.nextLine();
int n = sc.nextInt();
if(n < 1 || n > 10)
{
System.out.println("ENTER NUMBER BETEWEEN 1 TO 10");
System.exit(0);
}
String [] bloodComp = new String [n];
sc.nextLine();
for (int i = 0; i < n; i++)
{
bloodComp[i] = sc.nextLine();
}
CandidateCode obj = new CandidateCode();
for (String str : bloodComp)
{
obj.subsequence(str, virusComp);
}
}
}