Created
June 23, 2019 18:49
-
-
Save anushshukla/e6f60f8d352e18c19f18192b9b7ded39 to your computer and use it in GitHub Desktop.
getLongestSubStrings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function getLongestSubStrings($str) { | |
| $array = str_split($str); | |
| $arrayLen = sizeof($array) - 1; | |
| $noRepeatitiveSubStringFoundMsg = 'No repeated substring'; | |
| $longestRepeatedStringLen; | |
| $longestSubstringsFound = ''; | |
| for ($i=0; $i < $arrayLen / 2; $i++) { | |
| $groupOf = $i + 2; | |
| $longestRepeatedStringLen = | |
| $matchCount = false; | |
| for ($j=$i + 2; $j < $arrayLen - $groupOf; $j++) { | |
| if ($array[$i] === $array[$i + 2]) { | |
| $matchCount++; | |
| } | |
| } | |
| if ($matchCount === $groupOf) $longestSubstringsFound += ''; | |
| } | |
| $output = $longestSubstringsFound ?: $noRepeatitiveSubStringFoundMsg; | |
| return "Longest Repeated Substring in $str is: $output"; | |
| } | |
| var_dump(getLongestSubStrings('ABCDEFG')); // Longest Repeated Substring in ABCDEFG is: No repeated substring | |
| var_dump(getLongestSubStrings('banana')); // Longest Repeated Substring in banana is: ana | |
| var_dump(getLongestSubStrings('mom')); // Longest Repeated Substring in banana is: ana | |
| var_dump(getLongestSubStrings('abcpqrabpqpq')); // Longest Repeated Substring in abcpqrabpqpq are: ab, pq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment