title: Common Substring of two strings
body: <p>This particular interview-question stumped me:</p>
  
  <p><code>Given two Strings S1 and S2. Find the longest Substring which is a Prefix of S1 and suffix of S2</code>. </p>
  
  <p>Through Google, I came across the following solution, but didnt quite understand what it was doing. </p>
  
  <pre><code>public String findLongestSubstring(String s1, String s2) {
          List&lt;Integer&gt; occurs = new ArrayList&lt;&gt;();
          for (int i = 0; i &lt; s1.length(); i++) {
              if (s1.charAt(i) == s2.charAt(s2.length()-1)) {
                  occurs.add(i);
              }
          }
  
          Collections.reverse(occurs);
  
          for(int index : occurs) {
              boolean equals = true;
              for(int i = index; i &gt;= 0; i--) {
                  if (s1.charAt(index-i) != s2.charAt(s2.length() - i - 1)) {
                      equals = false;
                      break;
                  }
              }
              if(equals) {
                  return s1.substring(0,index+1);
              }
          }
  
          return null;
      }
  </code></pre>
  
  <p><strong>My questions:</strong></p>
  
  <ol>
  <li>How does this solution work?<br>
  <ul>
  <li>And how do you get to discovering this solution?   </li>
  </ul></li>
  <li>Is there a more intuitive / easier solution?</li>
  </ol>