【问题标题】:How to find sub string of a binary string in java如何在java中找到二进制字符串的子字符串
【发布时间】:2013-09-03 07:55:15
【问题描述】:
String s="101010101010";
String sub=""; //substring
int k=2;


   package coreJava;
    import java.util.Scanner; 
    public class substring {    
           public static void main(String args[])
           {
              String string, sub;
              int k, c, i;

              Scanner in = new Scanner(System.in);
              System.out.println("Enter a string to print it's all substrings");
              string  = in.nextLine();

              i = string.length();   

              System.out.println("Substrings of \""+string+"\" are :-");

              for( c = 0 ; c < i ; c++ )
              {
                 for( k = 1 ; k <= i - c ; k++ )
                 {
                    sub = string.substring(c, c+k);
                    System.out.println(sub);
                 }
              }
           }
        }
  1. 取一个二进制字符串 s="1010011010"; //等等
  2. 取一个变量k=2;
  3. 取另一个变量 i; //这是子字符串的长度(i>k)

现在我想找到上面字符串的子字符串,这样如果k=2,子字符串中1的数量必须是2,如果k=3,子字符串中1的数量必须是3等等……

Output should be like this: 
string s="1010011010" 
Enter value of k=2; 
Enter length of substring i=3; 
substring= 101 110 101 011

【问题讨论】:

  • 不要在 cmets 中这样写。编辑您的问题
  • 所以你想让我们做你的家庭作业?
  • 除了main方法的语法,你还试过什么?
  • 这是我的面试问题。我不知道我需要使用哪种方法。输出应该是这样的: string s="1010011010" Enter value of k=2;输入子串长度 i=3;子串= 101 110 101 011
  • 请帮助我如何修改上述编写的代码以满足我想要的条件。

标签: java string substring


【解决方案1】:

创建一个“窗口”,长度为您沿字符串移动的所需子字符串的长度,同时保持当前窗口中 1 的数量。每次迭代都将窗口沿一个移动,测试当前窗口之外的下一个字符,当前窗口中的第一个字符并相应地更新计数。在每次迭代期间,如果您的计数等于所需的长度,则从当前窗口打印子字符串。

public class Substring {

    public static void main(String[] args) {
        String str = "1010011010";

        int k = 2;
        int i = 3;

        printAllSubstrings(str, i, k);

    }

    private static void printAllSubstrings(String str, int substringLength, int numberOfOnes) {
        // start index of the current window
        int startIndex = 0;

        // count of 1s in current window
        int count = 0;

        // count 1s in the first i characters
        for (int a = 0; a < substringLength; a++) {
            if (str.charAt(a) == '1') {
                count++;
            }
        }

        while (startIndex < str.length() - substringLength + 1) {
            if (count == numberOfOnes) {
                System.out.print(str.substring(startIndex, startIndex + substringLength));
                System.out.print(" ");
            }
            // Test next bit, which will be inside the window next iteration
            if (str.length() > startIndex + substringLength && str.charAt(startIndex + substringLength) == '1') {
                count ++;
            }
            // Test the starting bit, which will be outside the window next iteration
            if (str.charAt(startIndex) == '1') {
                count --;
            }
            startIndex++;
        }   
    }
}

这个输出:

101 011 110 101 

【讨论】:

    【解决方案2】:

    遍历字符并计算一个的数量。如果计数器达到所需的数字,则停止迭代并将子字符串从索引 0 带到您得到的位置。

    String str = "010101001010";
    int count = 0;
    int k = 2;
    int i = 0;
    for (; i < str.length() && count < k; ++i)
    {
        if (str.charAt(i) == '1') count++;
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用正则表达式:

      public class BinaryString {
      
          public static void main(String[] args) {
              String binary = "11000001101110";
      
              int count = 3;
              String regEx = "1{" + count + "}";
      
              Pattern p = Pattern.compile(regEx);
              Matcher m = p.matcher(binary);
      
              if (m.find()) {
                  int startIndex = m.start();
                  System.out.println("MATCH (@index " + startIndex + "): "+ m.group());
              } else {
                  System.out.println("NO MATCH!");
              }
          }
      }
      

      输出

      MATCH (@index 10): 111
      

      【讨论】:

      • 哦...我的方法只是给你一个连续的 1。我误会你了。但也许这对所以有用。否则。
      • 但是由于正则表达式等同于确定性有限自动机,所以应该有一个正则表达式可以满足 OP 的要求……我脑子里有一个 DFA……但不是合适的正则表达式…… :D
      猜你喜欢
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2015-11-21
      • 2016-01-14
      • 1970-01-01
      • 2016-02-26
      • 2016-07-25
      相关资源
      最近更新 更多