Ok for my exercise I can\'t get it to do what is in red.....Ithink it is doing i
ID: 3613655 • Letter: O
Question
Ok for my exercise I can't get it to do what is in red.....Ithink it is doing it but... the online grader is saying that I have an "Incorrect result!" please help....The instructions are: (pattern matching) Write an efficient algorithm tocheck whether a string is in a text SUPPOSEALL CHARACTERS IN THE STRING ARE DISTINCT. Forsimplicity, assume the text is stored in a string, too.
public class Exercise23_3 { public static void main(String[] args) { System.out.println( match("hijklmnopqrstuvwxyz","lmno")); System.out.println( match("hijklmnopqrstuvwxyz","xyz")); System.out.println( match("hijklmnopqrstuvwxyz","hij")); System.out.println(match("hijklmnopqRstuvwxyz", "rst")); System.out.println(match("hijklmnopqrstuvwxyz", "kkk")); } public static int match(String s, String pattern) { int n =s.length(); int k =pattern.length(); for ( int i =0; i <=n-k; i++){ int p = 0; for(p=0; p < k; p++) { if (s.charAt(i+p) != pattern.charAt(p)) break; }
if(p == k) return i; } return-1; } } Ok for my exercise I can't get it to do what is in red.....Ithink it is doing it but... the online grader is saying that I have an "Incorrect result!" please help....
The instructions are: (pattern matching) Write an efficient algorithm tocheck whether a string is in a text SUPPOSEALL CHARACTERS IN THE STRING ARE DISTINCT. Forsimplicity, assume the text is stored in a string, too.
public class Exercise23_3 { public static void main(String[] args) { System.out.println( match("hijklmnopqrstuvwxyz","lmno")); System.out.println( match("hijklmnopqrstuvwxyz","xyz")); System.out.println( match("hijklmnopqrstuvwxyz","hij")); System.out.println(match("hijklmnopqRstuvwxyz", "rst")); System.out.println(match("hijklmnopqrstuvwxyz", "kkk")); } public static int match(String s, String pattern) { int n =s.length(); int k =pattern.length(); for ( int i =0; i <=n-k; i++){ int p = 0; for(p=0; p < k; p++) { if (s.charAt(i+p) != pattern.charAt(p)) break; }
if(p == k) return i; } return-1; } }
Explanation / Answer
if(p == k)
return i;
else
i=i+p;
}
return -1;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.