输入一个字符串,输出该字符串的所有子串(不能重复),所有子串个数小于100个
使用字符串 常用类的方法滑动窗口暴力解:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
* @Classname PrintSubString
* @Description :
* @Date 2022/4/19 20:21
* @Created by Twilight6
*/
public class PrintSubString {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(">>> ");
String s = scan.nextLine();
Set<String> strings = printSubString(s);
strings.forEach(System.out::println);
}
public static Set<String> printSubString(String str){
Set<String> set = new HashSet<>();
if (str == null || str.length() == 0){
return set;
}
int len = str.length();
int l = 0, r = 1;
while (r <= len){
for (int i = 0; i + r <= len; i++){
String substring = str.substring(l + i, r + i);
set.add(substring);
}
r++;
}
return set;
}
}
页:
[1]