forked from mithlesh4257/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNaive_Approach.java
More file actions
35 lines (28 loc) · 752 Bytes
/
Naive_Approach.java
File metadata and controls
35 lines (28 loc) · 752 Bytes
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
class Main
{
public static void search(String text, String pattern)
{
int lengthText = text.length();
int lengthPattern = pattern.length();
for(int i = 0; i <= lengthText - lengthPattern; i++)
{
int j;
for(j = 0; j < lengthPattern; j++)
if(text.charAt(i + j) != pattern.charAt(j))
break;
if(j == lengthPattern)
System.out.println("Pattern found at " + (i + 1));
}
}
public static void main(String[] args)
{
String text = "namanchamanbomanamansanam";
String pattern = "aman";
search(text, pattern);
}
}
/* Output
Pattern found at 2
Pattern found at 8
Pattern found at 17
*/