英文:
Maximum consecutive repeats of a substring in a given string using string library in Java
问题
我尝试找到给定字符串中子字符串的最大连续重复次数。我正在使用String库中的substring()、equals()和length()方法。但是,我没有得到正确的结果。这是我的代码-
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;
}
举个例子,如果 dna= "CAGCAGCAGTTCAGCAGCAGCAGTTCAGCAGCAG"
那么,子字符串 "CAG"
的最大连续重复次数 = 4 ---> 期望输出
但是对于这个子字符串或任何子字符串,这是我得到的结果-
最大重复次数 = 0
如果有人指出我哪里错了,将不胜感激
英文:
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
答案1
得分: 2
代码中的问题是您没有正确保存最大值。每当子字符串不等于"CAG"时,它都会被覆盖为值0。相反,您只需要在else条件中将count的值设置为0,而不是max=0。请检查以下代码。它应该适用于您。
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;
}
}
英文:
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;
}
}
答案2
得分: 0
问题在于您在比较是否不等于CAG
时,这是不必要的,导致您没有正确保存最大值。此外,在每次迭代中检查计数也是不必要的。
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 {
// 仅在未出现CAG时检查最大值。
if (count > max) {
max = count;
}
// 但无论如何都要重置计数器。
count = 0;
}
}
return Math.max(max, count);
}
另一种替代方法是使用正则表达式。
public static int maxRepeats(String dna) {
// 查找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();
}
}
// 最长的字符串必须是3的倍数,因此...
return longest / 3;
}
英文:
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;
}
</details>
# 答案3
**得分**: -1
方法 1:
```java
/**
* @param pattern 正在搜索的字符串
* @param text 在其中搜索的字符串
* @return 模式可以自我追加并保持为文本子字符串的最大次数
*/
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);
}
方法 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;
}
英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论