What's wrong with this code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
19th Oct 2019, 6:47 PM
Mayank Singh
Mayank Singh - avatar
25 Answers
- 1
import java.util.Scanner; public class naive { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1,s2; int flag = 0; System.out.println("enter the string"); s1 = sc.nextLine(); System.out.println(s1); System.out.println("enter the pattren"); s2 = sc.nextLine(); System.out.println(s2); int m = s1.length(); int n = s2.length(); if(m<=0 || n<=0 || n>m) flag = 0; else { for(int i=0;i<=m-n;i++) { if((s2.compareTo(s1.substring(i,i+n)))==0) { flag = 1; break; } else { flag =0; } } } if(flag == 0) System.out.println("Not found"); else if(flag == 1) System.out.println("Found"); } }
21st Oct 2019, 10:27 AM
Lakshmi kanth N V
Lakshmi kanth N V - avatar
+ 4
Max but you used contains method then how it is naive?
19th Oct 2019, 8:38 PM
A͢J
A͢J - avatar
+ 3
Thanks Kilowac works fine. 😊
19th Oct 2019, 7:07 PM
Mayank Singh
Mayank Singh - avatar
+ 3
Max I think you need this. I changed code and created a method which will return Boolean value. https://code.sololearn.com/c6kZN8kGkVVf/?ref=app
20th Oct 2019, 11:15 AM
A͢J
A͢J - avatar
+ 2
Use s1.contains(s2), its a boolean. you can just use one if else with the contains and you wont have to use flag
19th Oct 2019, 7:00 PM
Odyel
Odyel - avatar
+ 2
WRONG!!!
21st Oct 2019, 6:21 AM
Imran Alam
Imran Alam - avatar
+ 1
MAYANK SINGH NIKUMBH I'm not sure exactly why, but an empty string means nothing and "a" technically has nothing next to it so it's true. I don't know why, probably how scanner reads inputs.
19th Oct 2019, 7:24 PM
Odyel
Odyel - avatar
+ 1
AJ || ANANT || AC || ANANY || AY Yup you are right. I guess contains is not right for naive. I guess it should be any string matching method or char array comparison.
20th Oct 2019, 8:44 AM
Mayank Singh
Mayank Singh - avatar
+ 1
This code is shorter and it works https://code.sololearn.com/clc7tiE27n0A/?ref=app
20th Oct 2019, 10:16 AM
Tamar Peer
Tamar Peer - avatar
0
A sample input can be :- abab and acab it gives result found. But as we can see acab is not present in abab string.
19th Oct 2019, 6:49 PM
Mayank Singh
Mayank Singh - avatar
0
What was the error, i didn't run into any
19th Oct 2019, 6:51 PM
Odyel
Odyel - avatar
0
Kilowac try the sample input one
19th Oct 2019, 6:53 PM
Mayank Singh
Mayank Singh - avatar
19th Oct 2019, 7:03 PM
Odyel
Odyel - avatar
0
Kilowac when the s1 = a and s2 = blank .. then also it's found. "Like text is there but not pattern to be found in text is entered". This condition is also showing Found in your code. try input :- a 👈 blank line the Result is found.
19th Oct 2019, 7:15 PM
Mayank Singh
Mayank Singh - avatar
0
Kilowac Yup exactly string has null at it's last. That's why it's showing found. But that should not happen in reality. That's what I set the flags for.
19th Oct 2019, 7:29 PM
Mayank Singh
Mayank Singh - avatar
0
Ahh i c
19th Oct 2019, 7:34 PM
Odyel
Odyel - avatar
0
Max Why this is naive String match. It makes confusion.
19th Oct 2019, 7:42 PM
A͢J
A͢J - avatar
0
AJ || ANANT || AC || ANANY || AY It's naive because it just scrolls the pattern to be searched over the text in which it has to be searched. (Suppose like diagram on glossy paper sliding over main pic to match and complete the rest of diagram). There are 3 others which I know (algos) which are used for string matching :- 1) Rabin-Karp Algo 2) Finite Automata 3) KMP Algo These three preprocess the pattern string. And then search for the instances in text. This reduces the time complexity of the search. Hope this helps . 😊
19th Oct 2019, 8:33 PM
Mayank Singh
Mayank Singh - avatar
0
Tamar Peer for(int i=0;i<=m-n;i++) { if(s1.contains(s2)) { flag = true; break; } } What is the use of loop here? It can be like this also if(s1.contains(s2)) { System.out.println("found"); } else { System.out.println("Not Found"); } And also this is not NSM. It's a simple SubString checking.
20th Oct 2019, 10:40 AM
A͢J
A͢J - avatar
0
Tamar Peer It should not compare strings using contains method... Or else it would not be naive ... It should compare via string comparing methods or char array[ ]. I have changed my code... Help in that plz. •Also your code throws exception when either of s1 or s2 string is left blank. • Try input :- hey how are you ? e y it gives not found... even though it is present.
20th Oct 2019, 10:40 AM
Mayank Singh
Mayank Singh - avatar