I am solving below problem but all test cases not getting pass in java












0















There are a number of genes in a chromosome and your bioinformatic friend wants to check if a particular chromosome has a series of genes or not.
The sequence in which genes occur is important. You want to help your friend in this matter.
He gives you n chromosomes and a series of genes he wants to check. Help him identify whether these genes are present in the
chromosome in the given order.



Because you are not a biology student, he’s made things easier for you. He represents genes by a letter, number or special character present in ASCII.
Spaces do NOT represent a gene.



Example



If the chromosome is abdfgc, and the genes he’s querying for is abc, these are present (in the correct order) in the chromosome (marked in bold).
a*b*dfg*c*. However if the query is bca, this is not present in the correct order in the chromosome.



Input



The first line of input consists of an integer n which is the number of test cases.



Each test case consists of two lines of input:




  • The first line contains the chromosome

  • The second line contains the gene you are querying for
    Output


The output for that chromosome-gene pair should be “YES” if the genes, taken in order, are contained in the chromosome and “NO” otherwise.
The output should have n lines containing YES/NO.



Constraints



1 <= n <= 10000

1 <= |chromosome| <100

1 <= |gene| < |chromosome|


Sample Input



4
12sd78f
sf
12345efd
1e3d
ijkfgds
jkf
1111456
116


Sample Output



YES
NO
YES
YES


Code



import java.io.BufferedReader;
import java.io.InputStreamReader;

public class solution {

private static String checkGem(String ch, String gem) {

int prevIndex = 0;
for(int n=0; n < gem.length(); n++) {
if(ch.indexOf(gem.charAt(n), prevIndex)==-1) {
return "NO";
}
else {
prevIndex = ch.indexOf(gem.charAt(n), prevIndex);
}
}

return "YES";
}
public static void main(String args) throws Exception {

//Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String test = br.readLine();

int t = Integer.parseInt(test);

for(int i=0; i<t; i++) {
String ch = br.readLine();
String gem = br.readLine();
ch = ch.replaceAll("\s","");
gem = gem.replaceAll("\s","");
String ans = checkGem(ch, gem);
System.out.println(ans);
}

}
}









share|improve this question




















  • 1





    When you step through the code in your debugger, where does it not do what you expected?

    – Peter Lawrey
    Nov 22 '18 at 17:34











  • I did launch you code and all works well. Give an example of the test, that does not pass correctly.

    – Valentyn Hruzytskyi
    Nov 22 '18 at 17:51











  • prevIndex + 1 do not start next search from the same place

    – Dmitry Bychenko
    Nov 22 '18 at 17:56













  • where are you having problems

    – preciousbetine
    Nov 22 '18 at 17:59











  • I dont have test cases, only output i am getting as test case failed.

    – Nitin
    Nov 22 '18 at 18:01
















0















There are a number of genes in a chromosome and your bioinformatic friend wants to check if a particular chromosome has a series of genes or not.
The sequence in which genes occur is important. You want to help your friend in this matter.
He gives you n chromosomes and a series of genes he wants to check. Help him identify whether these genes are present in the
chromosome in the given order.



Because you are not a biology student, he’s made things easier for you. He represents genes by a letter, number or special character present in ASCII.
Spaces do NOT represent a gene.



Example



If the chromosome is abdfgc, and the genes he’s querying for is abc, these are present (in the correct order) in the chromosome (marked in bold).
a*b*dfg*c*. However if the query is bca, this is not present in the correct order in the chromosome.



Input



The first line of input consists of an integer n which is the number of test cases.



Each test case consists of two lines of input:




  • The first line contains the chromosome

  • The second line contains the gene you are querying for
    Output


The output for that chromosome-gene pair should be “YES” if the genes, taken in order, are contained in the chromosome and “NO” otherwise.
The output should have n lines containing YES/NO.



Constraints



1 <= n <= 10000

1 <= |chromosome| <100

1 <= |gene| < |chromosome|


Sample Input



4
12sd78f
sf
12345efd
1e3d
ijkfgds
jkf
1111456
116


Sample Output



YES
NO
YES
YES


Code



import java.io.BufferedReader;
import java.io.InputStreamReader;

public class solution {

private static String checkGem(String ch, String gem) {

int prevIndex = 0;
for(int n=0; n < gem.length(); n++) {
if(ch.indexOf(gem.charAt(n), prevIndex)==-1) {
return "NO";
}
else {
prevIndex = ch.indexOf(gem.charAt(n), prevIndex);
}
}

return "YES";
}
public static void main(String args) throws Exception {

//Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String test = br.readLine();

int t = Integer.parseInt(test);

for(int i=0; i<t; i++) {
String ch = br.readLine();
String gem = br.readLine();
ch = ch.replaceAll("\s","");
gem = gem.replaceAll("\s","");
String ans = checkGem(ch, gem);
System.out.println(ans);
}

}
}









share|improve this question




















  • 1





    When you step through the code in your debugger, where does it not do what you expected?

    – Peter Lawrey
    Nov 22 '18 at 17:34











  • I did launch you code and all works well. Give an example of the test, that does not pass correctly.

    – Valentyn Hruzytskyi
    Nov 22 '18 at 17:51











  • prevIndex + 1 do not start next search from the same place

    – Dmitry Bychenko
    Nov 22 '18 at 17:56













  • where are you having problems

    – preciousbetine
    Nov 22 '18 at 17:59











  • I dont have test cases, only output i am getting as test case failed.

    – Nitin
    Nov 22 '18 at 18:01














0












0








0








There are a number of genes in a chromosome and your bioinformatic friend wants to check if a particular chromosome has a series of genes or not.
The sequence in which genes occur is important. You want to help your friend in this matter.
He gives you n chromosomes and a series of genes he wants to check. Help him identify whether these genes are present in the
chromosome in the given order.



Because you are not a biology student, he’s made things easier for you. He represents genes by a letter, number or special character present in ASCII.
Spaces do NOT represent a gene.



Example



If the chromosome is abdfgc, and the genes he’s querying for is abc, these are present (in the correct order) in the chromosome (marked in bold).
a*b*dfg*c*. However if the query is bca, this is not present in the correct order in the chromosome.



Input



The first line of input consists of an integer n which is the number of test cases.



Each test case consists of two lines of input:




  • The first line contains the chromosome

  • The second line contains the gene you are querying for
    Output


The output for that chromosome-gene pair should be “YES” if the genes, taken in order, are contained in the chromosome and “NO” otherwise.
The output should have n lines containing YES/NO.



Constraints



1 <= n <= 10000

1 <= |chromosome| <100

1 <= |gene| < |chromosome|


Sample Input



4
12sd78f
sf
12345efd
1e3d
ijkfgds
jkf
1111456
116


Sample Output



YES
NO
YES
YES


Code



import java.io.BufferedReader;
import java.io.InputStreamReader;

public class solution {

private static String checkGem(String ch, String gem) {

int prevIndex = 0;
for(int n=0; n < gem.length(); n++) {
if(ch.indexOf(gem.charAt(n), prevIndex)==-1) {
return "NO";
}
else {
prevIndex = ch.indexOf(gem.charAt(n), prevIndex);
}
}

return "YES";
}
public static void main(String args) throws Exception {

//Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String test = br.readLine();

int t = Integer.parseInt(test);

for(int i=0; i<t; i++) {
String ch = br.readLine();
String gem = br.readLine();
ch = ch.replaceAll("\s","");
gem = gem.replaceAll("\s","");
String ans = checkGem(ch, gem);
System.out.println(ans);
}

}
}









share|improve this question
















There are a number of genes in a chromosome and your bioinformatic friend wants to check if a particular chromosome has a series of genes or not.
The sequence in which genes occur is important. You want to help your friend in this matter.
He gives you n chromosomes and a series of genes he wants to check. Help him identify whether these genes are present in the
chromosome in the given order.



Because you are not a biology student, he’s made things easier for you. He represents genes by a letter, number or special character present in ASCII.
Spaces do NOT represent a gene.



Example



If the chromosome is abdfgc, and the genes he’s querying for is abc, these are present (in the correct order) in the chromosome (marked in bold).
a*b*dfg*c*. However if the query is bca, this is not present in the correct order in the chromosome.



Input



The first line of input consists of an integer n which is the number of test cases.



Each test case consists of two lines of input:




  • The first line contains the chromosome

  • The second line contains the gene you are querying for
    Output


The output for that chromosome-gene pair should be “YES” if the genes, taken in order, are contained in the chromosome and “NO” otherwise.
The output should have n lines containing YES/NO.



Constraints



1 <= n <= 10000

1 <= |chromosome| <100

1 <= |gene| < |chromosome|


Sample Input



4
12sd78f
sf
12345efd
1e3d
ijkfgds
jkf
1111456
116


Sample Output



YES
NO
YES
YES


Code



import java.io.BufferedReader;
import java.io.InputStreamReader;

public class solution {

private static String checkGem(String ch, String gem) {

int prevIndex = 0;
for(int n=0; n < gem.length(); n++) {
if(ch.indexOf(gem.charAt(n), prevIndex)==-1) {
return "NO";
}
else {
prevIndex = ch.indexOf(gem.charAt(n), prevIndex);
}
}

return "YES";
}
public static void main(String args) throws Exception {

//Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String test = br.readLine();

int t = Integer.parseInt(test);

for(int i=0; i<t; i++) {
String ch = br.readLine();
String gem = br.readLine();
ch = ch.replaceAll("\s","");
gem = gem.replaceAll("\s","");
String ans = checkGem(ch, gem);
System.out.println(ans);
}

}
}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:47









Valentyn Hruzytskyi

321113




321113










asked Nov 22 '18 at 17:32









NitinNitin

61




61








  • 1





    When you step through the code in your debugger, where does it not do what you expected?

    – Peter Lawrey
    Nov 22 '18 at 17:34











  • I did launch you code and all works well. Give an example of the test, that does not pass correctly.

    – Valentyn Hruzytskyi
    Nov 22 '18 at 17:51











  • prevIndex + 1 do not start next search from the same place

    – Dmitry Bychenko
    Nov 22 '18 at 17:56













  • where are you having problems

    – preciousbetine
    Nov 22 '18 at 17:59











  • I dont have test cases, only output i am getting as test case failed.

    – Nitin
    Nov 22 '18 at 18:01














  • 1





    When you step through the code in your debugger, where does it not do what you expected?

    – Peter Lawrey
    Nov 22 '18 at 17:34











  • I did launch you code and all works well. Give an example of the test, that does not pass correctly.

    – Valentyn Hruzytskyi
    Nov 22 '18 at 17:51











  • prevIndex + 1 do not start next search from the same place

    – Dmitry Bychenko
    Nov 22 '18 at 17:56













  • where are you having problems

    – preciousbetine
    Nov 22 '18 at 17:59











  • I dont have test cases, only output i am getting as test case failed.

    – Nitin
    Nov 22 '18 at 18:01








1




1





When you step through the code in your debugger, where does it not do what you expected?

– Peter Lawrey
Nov 22 '18 at 17:34





When you step through the code in your debugger, where does it not do what you expected?

– Peter Lawrey
Nov 22 '18 at 17:34













I did launch you code and all works well. Give an example of the test, that does not pass correctly.

– Valentyn Hruzytskyi
Nov 22 '18 at 17:51





I did launch you code and all works well. Give an example of the test, that does not pass correctly.

– Valentyn Hruzytskyi
Nov 22 '18 at 17:51













prevIndex + 1 do not start next search from the same place

– Dmitry Bychenko
Nov 22 '18 at 17:56







prevIndex + 1 do not start next search from the same place

– Dmitry Bychenko
Nov 22 '18 at 17:56















where are you having problems

– preciousbetine
Nov 22 '18 at 17:59





where are you having problems

– preciousbetine
Nov 22 '18 at 17:59













I dont have test cases, only output i am getting as test case failed.

– Nitin
Nov 22 '18 at 18:01





I dont have test cases, only output i am getting as test case failed.

– Nitin
Nov 22 '18 at 18:01












1 Answer
1






active

oldest

votes


















0














You have a bug in your code. You can try this test case:



1
a
aaaaa


This one should be "NO", but your code output is "YES".



The problem is that you need to advance prevIndex it should be:



prevIndex = ch.indexOf(gem.charAt(n), prevIndex) + 1; 





share|improve this answer
























  • Thanks a lot its working Now

    – Nitin
    Nov 22 '18 at 19:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435907%2fi-am-solving-below-problem-but-all-test-cases-not-getting-pass-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have a bug in your code. You can try this test case:



1
a
aaaaa


This one should be "NO", but your code output is "YES".



The problem is that you need to advance prevIndex it should be:



prevIndex = ch.indexOf(gem.charAt(n), prevIndex) + 1; 





share|improve this answer
























  • Thanks a lot its working Now

    – Nitin
    Nov 22 '18 at 19:27
















0














You have a bug in your code. You can try this test case:



1
a
aaaaa


This one should be "NO", but your code output is "YES".



The problem is that you need to advance prevIndex it should be:



prevIndex = ch.indexOf(gem.charAt(n), prevIndex) + 1; 





share|improve this answer
























  • Thanks a lot its working Now

    – Nitin
    Nov 22 '18 at 19:27














0












0








0







You have a bug in your code. You can try this test case:



1
a
aaaaa


This one should be "NO", but your code output is "YES".



The problem is that you need to advance prevIndex it should be:



prevIndex = ch.indexOf(gem.charAt(n), prevIndex) + 1; 





share|improve this answer













You have a bug in your code. You can try this test case:



1
a
aaaaa


This one should be "NO", but your code output is "YES".



The problem is that you need to advance prevIndex it should be:



prevIndex = ch.indexOf(gem.charAt(n), prevIndex) + 1; 






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 18:08









Mahmoud HanafyMahmoud Hanafy

97011528




97011528













  • Thanks a lot its working Now

    – Nitin
    Nov 22 '18 at 19:27



















  • Thanks a lot its working Now

    – Nitin
    Nov 22 '18 at 19:27

















Thanks a lot its working Now

– Nitin
Nov 22 '18 at 19:27





Thanks a lot its working Now

– Nitin
Nov 22 '18 at 19:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435907%2fi-am-solving-below-problem-but-all-test-cases-not-getting-pass-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]