Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upLongestRepeatingSequence #1724
LongestRepeatingSequence #1724
Conversation
The longest sequence which has been repeated twice
| public class LongestRepeatingSequence { | ||
| //Checks for the largest common prefix | ||
| public static String lcp(String s, String t){ | ||
| int n = Math.min(s.length(),t.length()); | ||
| for(int i = 0; i < n; i++){ | ||
| if(s.charAt(i) != t.charAt(i)){ | ||
| return s.substring(0,i); | ||
| } | ||
| } | ||
| return s.substring(0,n); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| Scanner sc = new Scanner(System.in); | ||
| String str = sc.next(); | ||
| String lrs=""; | ||
| int n = str.length(); | ||
| for(int i = 0; i < n; i++){ | ||
| for(int j = i+1; j < n; j++){ | ||
|
|
||
| String x = lcp(str.substring(i,n),str.substring(j,n)); | ||
| if(x.length() > lrs.length()) lrs=x; | ||
| } | ||
| } | ||
| System.out.println("Longest repeating sequence: "+lrs); | ||
| } | ||
| } |
mertcandav
Oct 10, 2020
•
Hi. Whitespace patterns in the code are unbalanced. Please keep the number of spaces between operators and tokens.
| public class LongestRepeatingSequence { | |
| //Checks for the largest common prefix | |
| public static String lcp(String s, String t){ | |
| int n = Math.min(s.length(),t.length()); | |
| for(int i = 0; i < n; i++){ | |
| if(s.charAt(i) != t.charAt(i)){ | |
| return s.substring(0,i); | |
| } | |
| } | |
| return s.substring(0,n); | |
| } | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| String str = sc.next(); | |
| String lrs=""; | |
| int n = str.length(); | |
| for(int i = 0; i < n; i++){ | |
| for(int j = i+1; j < n; j++){ | |
| String x = lcp(str.substring(i,n),str.substring(j,n)); | |
| if(x.length() > lrs.length()) lrs=x; | |
| } | |
| } | |
| System.out.println("Longest repeating sequence: "+lrs); | |
| } | |
| } | |
| public class LongestRepeatingSequence { | |
| //Checks for the largest common prefix | |
| public static String lcp(String s, String t) { | |
| int n = Math.min(s.length(), t.length()); | |
| for (int i = 0; i < n; i++) { | |
| if (s.charAt(i) != t.charAt(i)) { | |
| return s.substring(0, i); | |
| } | |
| } | |
| return s.substring(0,n); | |
| } | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| String str = sc.next(); | |
| String lrs = ""; | |
| int n = str.length(); | |
| for (int i = 0; i < n; i++) { | |
| for (int j = i + 1; j < n; j++) { | |
| String x = lcp(str.substring(i, n), str.substring(j, n)); | |
| if (x.length() > lrs.length()) { | |
| lrs = x; | |
| } | |
| } | |
| } | |
| System.out.println("Longest repeating sequence: " + lrs); | |
| } | |
| } |
Himsahu
Oct 10, 2020
Author
Please accept all my pull request, so that I can participate in Hacktoberfest
|
Please add comments for the algorithm to be understood by all. |
|
LGTM 👍🏼 |
The longest sequence which has been repeated twice
Describe your change:
References
Checklist:
Fixes: #{$ISSUE_NO}.