Home java Java search algorithm Substrits

Java search algorithm Substrits

Author

Date

Category

How can one modify this algorithm so that after determining the index of the first entry of the substring in the string, it continues to search for the same substring in the row before it is completed, and did not go after the first finding.

Public int substring (String File, String Value) {
  int filelength = file.length ();
  INT VALUELENGTH = VALUE.LENGTH ();
  If (Valuelength & GT; Filelength) {
    Return -1;
  }
  Hashmap & lt; Character, Integer & GT; Cascade = New Hashmap & LT; Character, Integer & GT; ();
  For (int i = 0; i & lt; = 255; i ++) {
    Cascade.put ((Char) i, Valuelength);
  }
  For (int i = 0; i & lt; valuelength - 1; i ++) {
    Cascade.put (Value.Charat (I), Valuelength - I - 1);
  }
  int i = valuelength - 1;
  int j = i;
  int k = i;
  While (j & gt; = 0 & amp; & amp; i & lt; = filelength - 1) {
    j = valuelength - 1;
    k = i;
    while (j & gt; = 0 & amp; & amp; file.charat (k) == Value.Charat (j)) {
      k--;
      j--;
    }
    i + = Cascade.get (file.charat (i));
  }
  If (K & GT; = FileLength - Valuelength) {
    Return -1;
  } else {
    RETURN K + 1;
  }
}

Answer 1, Authority 100%

If the solution of this problem persists not a training or academic, but a practical goal, you can use regular expressions.

The following example that finds all the substring consisting of the letter “A” in the line and displays the position from which each of them begins.

import java.util.regex.matcher;
Import java.util.regex.pattern;
. . .
Pattern Pattern = Pattern.compile ("A +");
String S = "AAABBBGGARRPTVAAAAA0";
Matcher Matcher = Pattern.matcher (s);
While (Matcher.find ()) {
 System.out.printLN ("Position:" + Matcher.Start ());
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions