3

I would like to check if a pattern exists in a String using iteration.

Here is what I have currently but I keep getting false as a result of it.

public static void main(String args[]) {
        String pattern = "abc";
        String letters = "abcdef";

        char[] patternArray = pattern.toCharArray();
        char[] lettersArray = letters.toCharArray();

        for(int i = patternArray.length - 1; i<= 2; i++){
            for(int j = lettersArray.length - 1; j <= 5;j++){
                if(patternArray[i] == lettersArray[j]){
                    System.out.println("true");
                } else{
                    System.out.println("false");
                }
            }
        }
    }

Basically I would like to check if abc exists in the String abcdef

Note: I don't want to use regex because is too easy. I am trying to find a solution without it because I am curious how to do it with iteration.

5
  • @Ramanlfc because regex is too easy :) I am trying to find a solution without it because I am curious how to do it with iteration Commented Nov 24, 2015 at 3:18
  • What about using String#contains? Commented Nov 24, 2015 at 3:19
  • You are counting wrong in the for loop. You are setting i = patternArray.length - 1 which is 2, and then you increment while 2 is less than or equal to 2. This will terminate after one run. Commented Nov 24, 2015 at 3:19
  • 3
    @imbondbaby are you looking to find all matches, or does a single match suffice? Commented Nov 24, 2015 at 3:28
  • @imbondbaby What about my solution if you want to keep print statements inside the for loop? Commented Nov 24, 2015 at 3:38

5 Answers 5

3

Here’s a naive string matching program that will find all matches of the pattern.

Not recommended for anything practical, because of the O(mn) time complexity (m and n are the lengths of the search string and pattern respectively).

class Potato 
{
    public static void main(String args[])
    {
        char[] search = "flow flow flow over me".toCharArray();
        char[] pattern = "flow".toCharArray();

        for(int i = 0; i <= search.length - pattern.length; i++)
          // `-` don't go till the end of the search str. and overflow
        {
            boolean flag = true;
            for(int j=0; j < pattern.length; j++) 
            {
                if(search[i + j] != pattern[j])
                {
                    flag = false;
                    break;
                }
            }
          if (flag)
                System.out.println("Match found at " + i);

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Problem is you have two loops for each array. Here, you need single loop to traverse in both array using same index.

Comments

2

If you want to get all matches, i use a list to save matches addresses in the string.

    String pattern = "abc";
    String letters = "defabcdefabc";
    int i = 0;
    List<Integer> matches = new ArrayList();
    while (i <= letters.length() - pattern.length()) {
        if (letters.substring(i, i + pattern.length()).equals(pattern))
            matches.add(i);
        i += 1;
    }

You can iterate matches if you want to loop all matches with this solution. Edit:language changed

5 Comments

who asked you for python code? did not you see the tag java in the question? If it is easy to convert it to java, then post your code doing this easy task. Thanks
He doesn't ask how can i write this in java, he asks how can figure it out without using regex. I changed to code to java to, I hope there is no syntax mistake.
FYI, if OP post code in java, you should fix it in java or write a program in java if the tag java exists in the OP's tag.
yes if i had a chance i would. In next time I won't give answer in different language.
@RockOnGom the question is tagged as Java, not as Python. So, the standard is to post an answer using Java code, not Python, not C++, not JavaScript, not even Scala or another language that runs on the JVM. What you can do, instead,is to provide a pseudocode of the solution and state that your "code" is in fact pseudocode and that it can be implemented in the desired programming language, and then let the community upvote/downvote if it solves the problem.
2
public static Boolean patternFinder(String str, String pattern){
    for (int i = 0; i <= str.length()-pattern.length();i++){
        Boolean found = true;
        for (int f = 0; f < pattern.length();f++){
            if (pattern.charAt(f) != str.charAt(i+f)){
                found = false;
                break;
            }
        }
        if (found){
            return true;
        }
    }
    return false;
}

It's a very simple algorithm

basically, you loop through the string from the beginning and check if all the letters in the pattern are equal to the ones at that specific index.

Comments

1

Why not this:

public static void main(String args[]) {
    String pattern = "abc";
    String letters = "abcdef";

    char[] patternArray = pattern.toCharArray();
    char[] lettersArray = letters.toCharArray();
    boolean matched = false;
    for(int i = 0; i< lettersArray.length-patternArray.length && !matched; i++){
        for(int j = 0; j < patternArray.length;j++){
            if(patternArray[j] == lettersArray[i+j]&&j+1==patternArray.length){
                matched = true;
                System.out.println("true");
            }
            else if(i+1 == lettersArray.length-patternArray.length && j+1 == patternArray.length){
                System.out.println("false");
        }
    }
}

Comments

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.