Question

JAVA Your task is to implement a simple pattern-matching method. The method is supplied a pattern...

JAVA

Your task is to implement a simple pattern-matching method. The method is supplied a pattern to find (the “needle”) and a text to search (the “haystack”). The pattern will be a sequence of characters and, optionally, one or more dashes (-). A dash represents a wildcard, which matches any character. The method needs to return an array of all substrings of the haystack that matches the needle pattern – all characters are the same, or the pattern character is a dash. For example…

patternMatch("A--G-", "ACTGGTACTGA") -> ["ACTGG", "ACTGA"]

patternMatch("-GG-", "ACTGGTACTGA") -> ["TGGT"]

patternMatch("-GGC", "ACTGGTACTGA") -> []

For this problem you are NOT allowed to employ substring or pattern-matching methods – it must be implemented via looping over characters in the needle/haystack strings. You may create temporary methods/programs for your own debugging, but there is no main to complete/submit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here is the Java code with screenshot.

Just submit the method only for your assignment

====================================================================================

import java.util.ArrayList;
import java.util.Arrays;

public class PatternMatching {

    public static String[] patternMatch(String pattern, String text) {

        ArrayList<String> patterns = new ArrayList<>();
        int index = 1;
        for (int i = 0; i <= text.length() - pattern.length(); i++) {

            String subText = text.substring(i, i + pattern.length());
            boolean match = true;
            for (int j = 0; j < pattern.length(); j++) {
                if (subText.charAt(j) == pattern.charAt(j) || pattern.charAt(j) == '-'
                 ) {
                    continue;
                } else {
                    match = false;
                    break;
                }
            }
            if(match){
                patterns.add(subText);
            }


        }

        String [] matches = new String[patterns.size()];
        for(int pos=0;pos<patterns.size();pos++){
            matches[pos]=patterns.get(pos);
        }
        return matches;

    }

    public static void main(String[] args) {

        String[] matches = patternMatch("A--G-", "ACTGGTACTGA");
        System.out.println(Arrays.toString(matches));
         matches = patternMatch("-GG-", "ACTGGTACTGA");
        System.out.println(Arrays.toString(matches));
        matches = patternMatch("-GGC", "ACTGGTACTGA");
        System.out.println(Arrays.toString(matches));

    }
}

======================================================================================

import public class PatternMatching public static Stringtl patternMatch (String pattern, String text) 39 E public static void

import public class PatternMatching public static Stringtl patternMatch (String pattern, String text) 39 E public static void main (String[] args) 1 ) 40 50 51 PatternMatching main PatternMatching "C:Program Files\Java jdk-11\binljava.exe"-javaagent:C:\Program FilesJetBrains\Inte ACTGG, ACTGA] TGGT Process finished with exit code 0 r--I

Add a comment
Know the answer?
Add Answer to:
JAVA Your task is to implement a simple pattern-matching method. The method is supplied a pattern...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size...

    Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size and pattern, and checks if pattern exists inside size, if it exists then program returns index of first character of pattern inside size, otherwise it returns -1. The method should not use built-in methods such as indexOf , find, etc. Only charAt and length are allowed to use. Analyze the time complexity of your algorithm. Your solution is not allowed to be> = O...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT