简体   繁体   中英

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. take a binary string s="1010011010"; //etc
  2. take one variable k=2;
  3. take another variable i; //which is the length of the sub string(i>k)

now i want to find sub string of the above string, in such a way that if k=2,the number of 1's in sub string must be 2,if k=3,the number of 1's in substring must be 3 and so on...

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

Iterate over the characters and count the number of one's. If the counter reaches the desired number, stop iterating and take the substring from index zero to where you got.

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

Create a "window" the length of your desired substrings which you move along the string, maintaining a count of the number of 1s in your current window. Each iteration you move the window along one, testing the next character outside the current window, the first character in the current window and updating the count accordingly. During each iteration, if your count is equal to the desired length, print the substring from the current window.

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

This outputs:

101 011 110 101 

You could use regular expressions:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM