如何在字符串中找到四元组?

我有这个测试挑战,但我绝对是初学者,所以我坚持了下来,任何合适的解决方案将不胜感激!


我们会说字符串中的“四元组”是连续出现四次的字符。打印给定字符串中四元组的数量。四胞胎可以重叠。


提示:注意空格!


输入格式 带有字符串的单行。


输出格式 数字。


示例输入 abcXXXXXabc


示例输出 2


import java.util.*;


public class Quadruples{

    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);

        String s = sc.nextLine();

        char [] c = s.toCharArray();

        int j=0; int k=1; int m=j+2;


        for(int i=0; i<c.length-1; i++){

            if(c[j] != c[k]){

                j++; k++;

            }else{

                for(int l=0; l<c.length-1; l++){

                    if(c[j] == c[m]){

                        m++;

                    }else{   

                        continue;

                    }

                }

            }

        }

    }

}


慕姐8265434
浏览 161回答 5
5回答

蓝山帝景

您可以使用带有 String substring() 方法的 for 循环:&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; String str = input.nextLine();&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; for (int i = 0; i < str.length()-3; i++)&nbsp; &nbsp; &nbsp; &nbsp; if (str.substring(i, i+2).equals(str.substring(i+2, i+4))) count++;&nbsp; &nbsp; System.out.print(count);

慕的地10843

使用 for 循环的非常简单的暴力破解方法:&nbsp; &nbsp; String input = "abcXXXXXabc";&nbsp; &nbsp; // Counter to store the number of quadruples found&nbsp; &nbsp; int quadrupleCounter = 0;&nbsp; &nbsp; // Loop over each character in the input string (skip the last 3 to prevent going out of bounds)&nbsp; &nbsp; for(int i = 0;i < input.length() - 3; i++) {&nbsp; &nbsp; &nbsp; // Get the current char in the string + the next 3&nbsp; &nbsp; &nbsp; char c1 = input.charAt(i);&nbsp; &nbsp; &nbsp; char c2 = input.charAt(i + 1);&nbsp; &nbsp; &nbsp; char c3 = input.charAt(i + 2);&nbsp; &nbsp; &nbsp; char c4 = input.charAt(i + 3);&nbsp; &nbsp; &nbsp; // Check if all 4 characters are equal&nbsp; &nbsp; &nbsp; if(c1 == c2 && c2 == c3 && c3 == c4) {&nbsp; &nbsp; &nbsp; &nbsp; quadrupleCounter++; // Increase the counter&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(quadrupleCounter);

心有法竹

或者使用正则表达式(只是为了好玩):&nbsp; &nbsp; Pattern pattern = Pattern.compile("(.)\\1{3}");&nbsp; &nbsp; int quadrupleCounter = 0;&nbsp; &nbsp; Matcher matcher = pattern.matcher(input);&nbsp; &nbsp; if (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quadrupleCounter++&nbsp; &nbsp; &nbsp; &nbsp; } while (matcher.find(matcher.start()+1));&nbsp; &nbsp; }

潇湘沐

因此,如果我理解正确的话,“aaaabcccccc”意味着这个字符串中有 4 个四元组,例如,正如您所说的,它们可以重叠。所以这意味着 aaaa 是 1,前四个 cccc 是 2,然后从第二个 c 算起还有一个,所以是 3,然后从第三个 c 算起另一个是 4。如果是这样,那么下面的代码应该没问题因此,我们通过将字符串拆分为字符来创建一个 String 'stringArray'。然后我们使用第一个 for 循环(带有 i)来遍历所有字母,然后在该 for 循环中我们创建另一个 for 循环来检查当前字母是否等于接下来的 3 个字母--->如果是这样,然后我们增加 quadCount(我们找到了多少个四元组)。然后当 forloop 完成时,我们打印出 quadCount。您会看到我们检查 j=i+3 是否小于字符串数组的长度(在第二个 forloop 中),因为如果在我们检查的字符之后直到 letterArray 的末尾没有 3 个字符,那么肯定不会再有 3 个类似的。如果注意空格意味着“cc cc”中没有任何四元组,那么下面的代码是正确的。如果这意味着您不应该考虑空格,并且“cc cc”有一个四元组,那么只需首先调用 String 上的 .trim() 方法,如下所示:String quad= "aaaabccc ccc".trim();-->this将从字符串中删除(修剪)所有空格,然后代码就可以了。public static void main(String[] args) {&nbsp; &nbsp; String quad= "aaaabcccccc";&nbsp; &nbsp; int quadCount=0;&nbsp; &nbsp; String[] stringArray= quad.split("");&nbsp; &nbsp; for(int i=0; i<stringArray.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; String current= stringArray[i];&nbsp; &nbsp; &nbsp; &nbsp; int j=i+3;&nbsp; &nbsp; &nbsp; &nbsp; if(j<stringArray.length){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(current.equals(stringArray[i+1]) && current.equals(stringArray[i+2]) && current.equals(stringArray[i+3]) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quadCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Quadcounter final is: "+quadCount);&nbsp; &nbsp; }

冉冉说

未经测试,但我认为您可以从现在开始对其进行测试和更正。public class CheckQuadruple {&nbsp; &nbsp; private Boolean checkQuadrupleInString(String str, int i) {&nbsp; &nbsp; &nbsp; &nbsp; if (i + 3 >= str.length())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp;if (str.charAt(i) != str.charAt(i + 1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false;&nbsp; &nbsp; &nbsp; &nbsp;if (str.charAt(i + 1) != str.charAt(i + 2))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; if (str.charAt(i + 2) != str.charAt(i + 3))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; public int findQuadruple(String str) {&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < str.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (checkQuadrupleInString(str, i))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return sum;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java