Example :
"BAAABA"
should return 1, as we see that "A"
is repeated immediate 3 times."BAABAA"
should return 0, as we don't have any letter repeated immediate 3 times."BBBAAABBAA"
should return 2.Code which I have tried so far:
class Coddersclub {
public static void main(String[] args) throws java.lang.Exception {
String input = "Your String";
int result = 0;
int matchingindex = 0;
char[] iteratingArray = input.toCharArray();
for (int matchThisTo = 0; matchThisTo < iteratingArray.length; matchThisTo++) {
for (int ThisMatch = matchThisTo; ThisMatch < iteratingArray.length; ThisMatch++) {
if (matchingindex == 3) {
matchingindex = 0;
result = result + 1;
}
if (iteratingArray[matchThisTo] == iteratingArray[ThisMatch]) {
matchingindex = matchingindex + 1;
break;
} else {
matchingindex = 0;
}
}
}
System.out.println(result);
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SOTest {
final static String regex = "(\\w)\\1*";
public static void main(String[] args) {
final String inputString = "aaabbcccaaa";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(inputString);
int counter=0;
while (matcher.find()) {
String group = matcher.group(0);
if(group.length()==3) {
counter++;
System.out.println("Group found :: "+group);
}
}
System.out.println("Total pattern count :: "+counter);
}
}
Output:
Group found :: aaa
Group found :: ccc
Group found :: aaa
Total pattern count :: 3
Example :
"BAAABA"
should return 1, as we see that "A"
is repeated immediate 3 times."BAABAA"
should return 0, as we don't have any letter repeated immediate 3 times."BBBAAABBAA"
should return 2.Code which I have tried so far:
class Coddersclub {
public static void main(String[] args) throws java.lang.Exception {
String input = "Your String";
int result = 0;
int matchingindex = 0;
char[] iteratingArray = input.toCharArray();
for (int matchThisTo = 0; matchThisTo < iteratingArray.length; matchThisTo++) {
for (int ThisMatch = matchThisTo; ThisMatch < iteratingArray.length; ThisMatch++) {
if (matchingindex == 3) {
matchingindex = 0;
result = result + 1;
}
if (iteratingArray[matchThisTo] == iteratingArray[ThisMatch]) {
matchingindex = matchingindex + 1;
break;
} else {
matchingindex = 0;
}
}
}
System.out.println(result);
}
}
Since you've tagged this question with C#, here's a C# solution:
public static int CountTriples(string text)
{
int count = 0;
for (int i = 0; i < text.Length - 2; ++i)
{
if (text[i] == text[i+1] && text[i] == text[i+2])
{
++count;
i += 2;
}
}
return count;
}
[EDIT]
Someone other than the OP has removed the C# tag that was there when I wrote this answer. I'll leave this here anyway, since the code is trivially convertible to Java.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.