Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Part 2 of your question</h2> <p>Here is a shorter variant:</p> <pre><code>public String findLongestPrefixSuffix(String s1, String s2) { for( int i = Math.min(s1.length(), s2.length()); ; i--) { if(s2.endsWith(s1.substring(0, i))) { return s1.substring(0, i); } } } </code></pre> <p>I am using <code>Math.min</code> to find the length of the shortest String, as I don't need to and cannot compare more than that.</p> <p><code>someString.substring(x,y)</code> returns you the String you get when reading someString beginning from character <code>x</code> and stopping at character <code>y</code>. I go backwards from the biggest possible substring (<code>s1</code> or <code>s2</code>) to the smallest possible substring, the empty string. This way the first time my condition is true it will be biggest possible substring the fulfills it.</p> <p>If you prefer you can go the other way round, but you have to introduce a variable saving the length of the longest found substring fulfilling the condition so far:</p> <pre><code>public static String findLongestPrefixSuffix(String s1, String s2) { if (s1.equals(s2)) { // this part is optional and will return s1; // speed things up if s1 is equal to s2 } // int max = 0; for (int i = 0; i &lt; Math.min(s1.length(), s2.length()); i++) { if (s2.endsWith(s1.substring(0, i))) { max = i; } } return s1.substring(0, max); } </code></pre> <p>For the record: You could start with <code>i = 1</code> in the latter example for a tiny bit of extra performance. On top of this you can use <code>i</code> to specify how long the suffix has at least to be you want to get. ;) If you writ <code>Math.min(s1.length(), s2.length()) - x</code> you can use <code>x</code> to specify how long the found substring may be at most. Both of these things are possible with the first solution, too, but the min length is a bit more involving. ;)</p> <hr> <h2>Part 1 of your question</h2> <p>In the part above the <code>Collections.reverse</code> the author of the code searches for all positions in <code>s1</code> where the last letter of <code>s2</code> is and saves this position.</p> <p>What follows is essentially what my algorithm does, the difference is, that he doesn't check every substring but only those that end with the last letter of <code>s2</code>.</p> <p>This is some sort of optimization to speed things up. If speed is not that important my naive implementation should suffice. ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload