0

say we have a string with these characters "ABGCCFFGTBG"

then we have another string with characters "GECCCDOABG"

So the pattern is the prefix and suffix but if your given strings larger then this but have common prefix and suffix patterns how to pull those out into a substring in java. Keep in mind we dont always know the characters in the string were getting we just know there is a pattern in it.

my start is something like this

for(int i = 0. i < strA.length(); i++)
{
    for(int j = 0; j < strB.length(); j++)
    {
       if(strA.charAt(i) == strB.charAt(j))
       {
          String subPattern = strA.substring(0,i);
          String subPattern2 = strB.substring(0,j);
       }
    }
}  

but this doesn't work. Any ideas?

1
  • an example is pulling out the ABG in the string because its in both as a pattern into a substring Commented Nov 16, 2014 at 17:01

2 Answers 2

1

Try to select best-matched pattern at first:

public static void main(String[] args) {
    String strA = "ABGCCFFGTBG";
    String strB = "GECCCDOABG";
    System.out.println("Pattern: " + findPattern(strA, strB));
}

public static String findPattern(String strA, String strB) {
    for (int length = Math.min(strA.length(), strB.length()); length > 0; length--) {
        for (int i = 0; i <= strA.length() - length; i++) {
            String pattern = strA.substring(i, i + length);
            if (strB.contains(pattern)) {
                return pattern;
            }
        }
    }
    throw new NoSuchElementException("No common pattern between " + strA + " and " + strB);
}

Output:

Pattern: ABG
Sign up to request clarification or add additional context in comments.

6 Comments

the only issue is i need to get values like how ABG are common pattern in both but this implementation only gives me C
Use return instead of println and you'll get the best match value.
how? its not value returning
Is it a joke? just modify this code into your own method with arguments (String strA, String strB) and return type String.
it does work but only using one return gives me the value B I still need ABG. Only one return statement allowed
|
0

this solution will find a pattern, no matter where it is in the string:

public static void main(String[] args) {

    String strA = "uioABCDqwert";
    String strB = "yxcvABCDwrk";

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < strA.length(); i++) {
        for (int j = 0; j < strB.length(); j++) {
            if (strA.charAt(i) == strB.charAt(j)) {
                sb.append(strB.charAt(j));
                i++;
            }
        }
        if (sb.length() > 0)
            break;
    }

    System.out.println(sb.toString());
}

this should give you an idea how it could be done

1 Comment

what if its a value returning method like String or if you cant use break statement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.