Pattern Matching in Java without using Built-in Method | Interview Coding Question

preview_player
Показать описание
String Pattern Matching without using inbuilt methods

Follow on Facebook:

Subscribe to our other channel:
Telusko Hindi :
Рекомендации по теме
Комментарии
Автор

for(int j=0;k=i;j<plen;k++, j++)
{
if
count++;
else
count =0;
}
this works fine as we need to set the couter value again to zero so that next matching begins with iterator value of 0.

divyamehta
Автор

Pattern/String matching is so important in bioinformatics, there are famous algorithms like Boyer-Moore. Always kind of confused me, but this explanation is a good start in better understanding - thanks a lot for this approach!

health.upgradedbyscience.
Автор

here is a simple implementation for the same approach :
String s1 = "navinreddy";
String s2 = "vin";
int i = 0;
boolean res = false;
for (int j = 0; j < s1.length(); j++) {
if (s2.charAt(i) == s1.charAt(j)) {
i++;
if (i >= s2.length()) {
= true;

}
}
}

if (res)
System.out.println(true);
else


time complexity: O(n)
space complexity: O(1)

younessben
Автор

ur code is wrong gives exceptions
for(int j=i, k=0;j<tlen && k<plen;j++, k++)
{

// this code gives required result
}

dhanalakshmissec
Автор

One more implementation from myself which requires less code. I've solved this problem using subString(). Sorry, the code is in Kotlin, but I guess you can understand:

fun main() {
val text = "Naveen"
val pattern = "een"

println("${contains(text, pattern)}")
}

fun contains(text: String, pattern: String): Boolean {
if (text.length < pattern.length) {
return false
}

if (text.length == pattern.length) {
return text == pattern
}

for (i in 0 until text.length - pattern.length) {
println(text.substring(i, i + pattern.length))
if (text.substring(i, i + pattern.length) == pattern) {
return true
}
}

return false
}

haykmkrtchyan
Автор

optimize solution
int x =0;
for (int i=0 ;i <str.length() ; i++) {


x++;
}
if(x==str2.length()) break;

}
if(x==str2.length()) System.out.println(true);
else System.out.println(false);

maddy__
Автор

Hi Naveen... If c & p values are not matching in the second if condition.. we need to loop the code... Otherwise it's get failed

ahmedsyed
Автор

public static int match(String str, String ptrn) {
int sizeStr = str.length();
int sizePtrn = ptrn.length();
int count=0, pattern=0;
for(int i=0;i<sizeStr;i++) {
{
count++;
for(int j=i+1, n=1;j<i+sizePtrn;j++, n++) {
{
count++;
}
}
if(count==sizePtrn) {
pattern++;
}
}
count=0;
}
return pattern;
}

ysagar
Автор

i think we should place count=0 in outer for loop else suppose there is for this your code will print pattern found because it is not getting back to zero on failed result

ravi
Автор

what if the pattern is like
a*, *a, *a*
i/p: {"apple", "banana"}

1)if the pattern entered is a*
o/p: apple (starting with a)
2)if the pattern entered is *a*
o/p:banana (words that containing a)
3)if the pattern entered is *a
o/p:banana (words ending with a)

can u help me out to write java code for this problem without using inbuilt functions.

satishr
Автор

public class a {


public static void main(String[] args) {

String b = "i facebook you";
String c = "book u";
int d, e, j;
int count=0;

d = b.length();
e = c.length();
for(int i=0;i<d;i++){
j=0;
while(j<e){
if(b.charAt(i)== c.charAt(j)){
i++;
j++;
count++;

}
else break;


}

}

if(count == e)
System.out.println("found");
else
System.out.println("not found");






}

}

rajatsinha
Автор

count needs to be reinitialised to 0 in every iteration of "i" loop, else this would not work.

starringharsh
Автор

this logic is not working for duplicate characters in string

girishwarpatil
Автор

Many issues with your code:
1- tlen - plen (you are assuming that the first string is longer, it could be shorter then your code will break)

2- your code checks the first letter of pattern only once, and that's a mistake:
let's make text = "navinreddyrdd"; and pattern = "rdd" ==> your code will break because the way you iterate is wrong. once you find the first letter of pattern in text you jump into the rest of the letters, but you don't go back and start over. the pattern can exist beyond the first letter:
example: let say we are looking for "red" in "ABCDRABCDred". applying your code will yields to pattern not found.

3- last issue with your code is your iteration with k++, not paying attention that the value of k will exceed the length of pattern, pattern.charAt(k) will return null.
"red" length = 3, if k = 4 then charAt(4) is null. when you have p = null, p is char remember, it cannot be null.

akarafi
Автор

Thanks for the video its very helpful. do u any idea about how can i write this algorithm in c++

iremdinc
Автор

This code will handle duplicates

public class PatternMatch {

public static void main(String[] args) {

String text = "texttouseinpatternmatch";
String pattern = "te";
int tlen = text.length();
int plen = pattern.length();
int count = 0;
int patternCount = 0;
char t, p;

for(int i=0; i <= tlen-plen; i++) {
t = text.charAt(i);
p = pattern.charAt(0);
if (t == p) {
count++;
for(int j= i+1, k=1; k < plen; j++, k++) {
t = text.charAt(j);
p = pattern.charAt(k);
if (t == p)
count++;
}
if (count == plen)
patternCount++;
}
else {
count = 0;
}
}

if (patternCount > 0)
System.out.println("Pattern found " + patternCount + " times");
else
System.out.println("Not Found!");
}

}

w.d.tchathurange
Автор

should add else in the line no.31.
else
{
count=0;
}

venkatachalams
Автор

This logic is not work in duplicate char... str = Sadekujjaman; pattern = jam;

sadekujjamansaju
Автор

This code will not work for some words
For example : text = "gagan";
Pattern = "gan";
Please help on this.

gagankumarjk
Автор

How about this, much simpler:
private static boolean match(String text, String match) {

char[] inputArray = text.toCharArray();
char[] matchArray = match.toCharArray();

for (int i = 0; i < text.length(); i++) {

if (inputArray[i] == matchArray[0]) {

int textIndex = i;
int matchIndex = 0;
int matchLength = 0;

while (inputArray[textIndex] == matchArray[matchIndex]) {



(matchLength == match.length()) {
true;

}
}
}
return false;
}

raghav
visit shbcf.ru