这是给定问题的完整文本:
给定一个字符串 s 和一个整数 k ,编写一个函数,以便它找到字典中最小和最大的子串长度 k
示例输入: welcometojava 3
示例输出: AVA WEL
答案 0 :(得分:5)
以下是适合您的代码:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
int length = sc.nextInt();
String smallest="";
String largest="";
for(int i = 0;i<=inputString.length()-length;i++){
String subString = inputString.substring(i,i+length);
if(i == 0){
smallest = subString;
}
if(subString.compareTo(largest)>0){
largest = subString;
}else if(subString.compareTo(smallest)<0)
smallest = subString;
}
System.out.println(smallest);
System.out.println(largest);
}
答案 1 :(得分:0)
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
int length = sc.nextInt();
String smallest="";
String largest="";
for(int i = 0;i<=inputString.length()-length+1;i++){
String subString = inputString.substring(i,i+length);
if(i == 0){
smallest = subString;
}
if(subString.compareTo(largest)>0){
largest = subString;
}else if(subString.compareTo(smallest)<0)
smallest = subString;
}
System.out.println(smallest);
System.out.println(largest);
}
答案 2 :(得分:0)
我如何从先前的子字符串中获取子字符串的想法:
/*Lexicographically smallest and largest substring of size k*/
class LexicographicallyString{
public static void main(String[] args) {
String s="welcometojava";
help(s,3);
}
public static void help(String s,int n){
String cs=s.substring(0,n);
String min=cs;
String max=cs;
for(int i=n;i<s.length();i++){
cs=cs.substring(1,n)+s.charAt(i);
if(max.compareTo(cs)<0){
max=cs;
}
if(min.compareTo(cs)>0){
min=cs;
}
}
System.out.println(min+" "+max);
}
}
答案 3 :(得分:0)
您可以按照以下步骤操作,
使用初始字符串初始化最小和最大字符串,即
s.substring(0, k)
从索引1到小于字符串的长度遍历字符串。
a。检查是否为s.length() - loop counter >= k
,这意味着我们将比较的下一个字符串为length >= k
。此条件对于避免 IndexOutOfBoundsException 很重要。
b。比较使用s.substring(i, i + k)
获得的子字符串是否按字母顺序小于或大于我们在步骤1中初始化的最小和最大子字符串并相应地分配值。
检查以下代码段,
String currentStr = s.substring(0, k);
smallest = currentStr;
largest = currentStr;
for (int i = 1; i < s.length(); i++) {
if (s.length() - i >= k) { // to avoid IndexOutOfBoundsException
String temp = s.substring(i, i + k);
if (temp.compareTo(smallest) < 0) {
smallest = temp;
}
if (temp.compareTo(largest) > 0) {
largest = temp;
}
}
}
您可以在下面的链接中找到完整的代码,
答案 4 :(得分:-1)
// Assign the string we want to process to a variable
inputStr = "welcometojava"
// define and array for each sub-string.
String[] substrs = new string[input.length()-2];
// Fill our container by iterating from 0 up through the place of the last sub string.
for(int i = 0; i < inputStr.length()-2; i++){
// assign array members sub strings from input string for each starting position
substrs[i] = inputStr.substring(i,i+3);
}
// Sort and print your results.
Arrays.sort(substrs);
for(int i = 0; i < args.length; i++)
System.out.println(substrs[i]);
答案 5 :(得分:-1)
下面是代码->
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
//String[] strArray = new String[]{};
String subString = null;
for(int i=0; i<s.length()-k+1;i++){
subString = s.substring(i,i+k);
if(i==0){
smallest = subString;
largest = subString;
}
else if(subString.compareTo(smallest)<0){
smallest = subString;
}else if(subString.compareTo(largest)>0){
largest = subString;
}
}
// Complete the function
// 'smallest' must be the lexicographically smallest substring of length 'k'
// 'largest' must be the lexicographically largest substring of length 'k'
return smallest + "\n" + largest;
}