Need help with Naive Approach for searching in JAVA

Problem Statement

Given a string S and a pattern P both of lowercase characters. The task is to check if the given pattern P exists in the given string S or not.

Input

First line of input contains number of testcases T. For each testcase, first line will the string and second line will be the pattern to be searched.

Constraints:
1 <= T <= 100
1 <= |S|, |P| <= 1000

Output

For each testcase, print “Yes” if pattern exists or “No” if doesn’t.

Code for the question :

public class Naive {

public static void search(String str, String pattern) {
            int n = str.length(); 
            int m = pattern.length(); 
    
    for (int s = 0; s <= n - m; s++) { 
         
        for (int j = 0; j < m; j++)
            if (str.charAt(s+j) != pattern.charAt(j) )
                System.out.println("No");
                break;
              if (j == m)
                System.out.println(" Yes");
                     } 
} 

public static void main(String[] args) 
{  Scanner sc = new Scanner(System.in);

    String txt = sc.nextLine();
    String pat = sc.nextLine(); 
   
    search(txt, pat); 
}

}

Explain the algorithm you are using for the code you wrote. Then, we can tell you if the code contains the algorithm you are trying to use.

1 Like

I will traverse the entire string to check if there are any matching patterns of my string . if there are any matches we will print at what index they are present if no match then we will break it.

Your code is not properly indented. Indent it and I think you will see some issues. Are you getting any errors when trying to compile?

Can you provide a link to the actual problem page?

it gives the index number when i try to run it but i want the output as Yes or No for pattern matching and it is not working for That.

I meant a link to the Newton School site page.

If you run this you will get expected output

/*package whatever //do not write package name here /
import java.util.
;

public class Naive {

public static void search(String str, String pattern) {
            int n = str.length(); 
            int m = pattern.length(); 
    
    for (int s = 0; s <= n - m; s++) { 
         int j;
        for ( j = 0; j < m; j++)
        
            if (str.charAt(s+j) != pattern.charAt(j) )
            break;
              if (j == m)
                System.out.println(s);
                     } 
} 

public static void main(String[] args) 
{  Scanner sc = new Scanner(System.in);

    String txt = sc.nextLine();
    String pat = sc.nextLine(); 
   
    search(txt, pat); 
}

}

https://my.newtonschool.co/playground/code/jdvnc05izw4w/

The code you wrote above and the code shown in the editor on Newton School is not the same.

yes I know I try code first on other ide before running on newton portal and I tried to edit it on newton portal to get expected output

Please update the Newton School code (where I can actually test it) to be what code you actually want to use.

Also, please properly indent your code.

Thank you.

1 Like

https://my.newtonschool.co/playground/code/jdvnc05izw4w/

hi there,

your code isn’t compiling because your for loop is missing the declaration for j
Main.java:19: error: cannot find symbol if (j=m){ ^ symbol: variable j location: class Main 1 error

Edit: as Randell mentioned (and I asked you before in a previous thread), when posting on the forum, your code should be indented correctly.
It still isn’t.
Let us know if you don’t know what that means.

Also consider adding comments. It will help people read your code and help you become a better developer.

Can you tell me how to indent code properly I am beginner so you can teach me.

2 Likes