簡體   English   中英

如何在Java中查找二進制字符串的子字符串

[英]How to find sub string of a binary string in java

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

遍歷字符並計數一個字符的數量。 如果計數器達到所需的數目,請停止迭代,並將子串從索引零移到您得到的位置。

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++;
}

創建一個“窗口”,該窗口沿字符串移動所需的子字符串的長度,並在當前窗口中保持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 

您可以使用正則表達式:

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!");
        }
    }
}

OUTPUT

MATCH (@index 10): 111

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM