问题描述
方法一
package com.gxwz.lanqiaobei;
import java.util.ArrayList;
import java.util.List;
/**
* 不同子串
* @author com
*
*/
public class JavaB2019 {
// static String str = "aaab"; //substring[beginIndex,endindex)左闭右开
static String str = "0100110001010001";
public static void main(String[] args) {
f(str);
}
public static int f(String s) {
int length = s.length();
int count = 1; //计数器,字符串本身就是一个非空子串,故初始值一
int extent = 1; //子串的长度
String sub = null;
List<String> list = null;
while(extent<length) { //while循环子串的长度
list = new ArrayList<String>();
for(int i=0;i<=length-extent;i++) { //for循环不同位置子串的长度
sub = s.substring(i, i+extent); //子串为每次循环不同位置不同长度的字符串
boolean flag = false; //判断该字符在list中是否存在
for (String sub1 : list) {
if(sub1.equals(sub)) {
flag = true;
}
}
//如果字符串不相同,则添加字符串
if(flag == false) {
list.add(sub);
count++;
}
}
System.out.print(extent+"位数的:");
for (String string : list) {
System.out.print(string+" ");
}System.out.println();
extent++;
}
System.out.println(length+"位数的:"+s);
System.out.println("一共"+count+"种");
return count;
}
}
运行结果
1位数的:0 1
2位数的:01 10 00 11
3位数的:010 100 001 011 110 000 101
4位数的:0100 1001 0011 0110 1100 1000 0001 0010 0101 1010
5位数的:01001 10011 00110 01100 11000 10001 00010 00101 01010 10100 01000
6位数的:010011 100110 001100 011000 110001 100010 000101 001010 010100 101000 010001
7位数的:0100110 1001100 0011000 0110001 1100010 1000101 0001010 0010100 0101000 1010001
8位数的:01001100 10011000 00110001 01100010 11000101 10001010 00010100 00101000 01010001
9位数的:010011000 100110001 001100010 011000101 110001010 100010100 000101000 001010001
10位数的:0100110001 1001100010 0011000101 0110001010 1100010100 1000101000 0001010001
11位数的:01001100010 10011000101 00110001010 01100010100 11000101000 10001010001
12位数的:010011000101 100110001010 001100010100 011000101000 110001010001
13位数的:0100110001010 1001100010100 0011000101000 0110001010001
14位数的:01001100010100 10011000101000 00110001010001
15位数的:010011000101000 100110001010001
16位数的:0100110001010001
一共100种
解题思路
子串是1到字符串的长度,本身也算是一个唯一的子串,就没有放进循环里面判断了,所以count初始值为1。
extend表示子串的长度,先用while循环得到每个不同子串的长度,然后再用一个for循环和String的substring()方法
遍历得到每种长度的每个子串,然后再遍历ArrayList数组,用boolean类型的flag判断当前遍历的子串ArrayList里面是否存在。
存在则flag标为true,然后再用list.add()添加该子串,每次添加count++计数,循环结束便可以得到所求的非空子串的长度了。
--------------------------------------------------------分割线----------------------------------------------------------------
方法二
package com.gxwz.lanqiaobei;
import java.util.Set;
import java.util.TreeSet;
public class DifferentSubstring {
static String str = "0100110001010001";
static Set<String> set = new TreeSet<String>();
public static void main(String[] args) {
inquery();
show();
}
public static void inquery() {
int length = str.length();
for(int i=1;i<length;i++) {
for(int j=0;j<length-i+1;j++) {
set.add(str.substring(j,j+i));
}
}
set.add(str); // 本身也是一种情况,切勿遗漏
}
public static void show() {
int n = 1;
for (String l : set) {
System.out.printf("第%d个:%s\n",n++,l);
}
System.out.println("一共"+set.size()+"种");
}
}
运行结果
第1个:0
第2个:00
第3个:000
第4个:0001
第5个:00010
第6个:000101
第7个:0001010
第8个:00010100
第9个:000101000
第10个:0001010001
第11个:001
第12个:0010
第13个:00101
第14个:001010
第15个:0010100
第16个:00101000
第17个:001010001
第18个:0011
第19个:00110
第20个:001100
第21个:0011000
第22个:00110001
第23个:001100010
第24个:0011000101
第25个:00110001010
第26个:001100010100
第27个:0011000101000
第28个:00110001010001
第29个:01
第30个:010
第31个:0100
第32个:01000
第33个:010001
第34个:01001
第35个:010011
第36个:0100110
第37个:01001100
第38个:010011000
第39个:0100110001
第40个:01001100010
第41个:010011000101
第42个:0100110001010
第43个:01001100010100
第44个:010011000101000
第45个:0100110001010001
第46个:0101
第47个:01010
第48个:010100
第49个:0101000
第50个:01010001
第51个:011
第52个:0110
第53个:01100
第54个:011000
第55个:0110001
第56个:01100010
第57个:011000101
第58个:0110001010
第59个:01100010100
第60个:011000101000
第61个:0110001010001
第62个:1
第63个:10
第64个:100
第65个:1000
第66个:10001
第67个:100010
第68个:1000101
第69个:10001010
第70个:100010100
第71个:1000101000
第72个:10001010001
第73个:1001
第74个:10011
第75个:100110
第76个:1001100
第77个:10011000
第78个:100110001
第79个:1001100010
第80个:10011000101
第81个:100110001010
第82个:1001100010100
第83个:10011000101000
第84个:100110001010001
第85个:101
第86个:1010
第87个:10100
第88个:101000
第89个:1010001
第90个:11
第91个:110
第92个:1100
第93个:11000
第94个:110001
第95个:1100010
第96个:11000101
第97个:110001010
第98个:1100010100
第99个:11000101000
第100个:110001010001
一共100种