Reputation: 81
I'm trying to find the maximum consecutive repeats of a substring in a given string. I'm using substring(), equals(), and length() methods from the String library. However, I don't get the correct result. Here's my code-
public static int maxRepeats(String dna) {
int max = 0;
int count = 0;
for (int i = 0; i < dna.length() - 3; i++) {
String s = dna.substring(i, i + 3);
if (s.equals("CAG")) {
count++;
i += 2;
}
if (!s.equals("CAG")) {
max = count;
count = 0;
}
}
return max;
}
Let for example dna= "CAGCAGCAGTTCAGCAGCAGCAGTTCAGCAGCAG"
Then, max consecutive repeats of substring "CAG"
= 4 ---> expected output
But for this substring or any substring, this is the result I get-
max repeats = 0
Would be grateful if someone pointed out where am I wrong :-)
Upvotes: 1
Views: 1487
Reputation: 348
Method 1:
/**
* @param pattern string being searched
* @param text string being searched in
* @return max number of times pattern can be self-appended and remains a
* substring of text
*/
int maxRepeats(String pattern, String text) {
int max = 0;
int count = 0;
int i = 0;
while (i <= text.length() - pattern.length()) {
String s = text.substring(i, i + pattern.length());
if (s.equals(pattern)) {
count++;
i += pattern.length();
} else {
max = Math.max(max, count);
count = 0;
i++;
}
}
return Math.max(max, count);
}
Method 2:
int maxRepeats(String pattern, String text) {
String s = pattern;
int max = 0;
while (s.length() <= text.length()) {
if (text.contains(s)) {
max++;
s += pattern;
} else {
break;
}
}
return max;
}
Upvotes: -1
Reputation: 40034
The problem is you are comparing for not equal to CAG
and it is not necessary, resulting in you not saving the max correctly. Also, it is not necessary to check for count on each iteration.
public static int maxRepeats(String dna) {
int max = 0;
int count = 0;
for (int i = 0; i <= dna.length() - 3; i++) {
String s = dna.substring(i, i + 3);
if (s.equals("CAG")) {
count++;
i += 2;
} else {
// only check max when CAG is not present.
if (count > max) {
max = count;
}
// but reset counter regardless.
count = 0;
}
}
return Math.max(max,count);
}
Another alternative is to use regular expressions.
public static int maxRepeats(String dna) {
// find the longest repeats of CAG
Matcher m = Pattern.compile("(CAG)*").matcher(dna);
int longest = 0;
while (m.find()) {
String f = m.group();
if (f.length() > longest) {
longest = f.length();
}
}
// the longest string must be divisible by 3 so...
return longest/3;
}
Upvotes: 0
Reputation: 413
The problem with your code was you are not saving the max value properly . It was getting overridden to value 0 when ever the substring is not equal to "CAG". Instead you only need to set the value of count=0 in else condition and not max =0. Check this code. It should work for you
int max = 0;
int count = 0;
for (int i = 0; i < dna.length() - 3; i++) {
String s = dna.substring(i, i + 3);
if (s.equals("CAG")) {
count++;
i += 2;
} else {
count=0;
}
if (count>max) {
max = count;
}
}
Upvotes: 2