在这些 HackerRank Java 挑战之一中,有一个问题被定义为:
问题
我们定义以下术语:
Lexicographical Order,也称为字母或字典顺序,按如下方式对字符进行排序:A < B < ...< Y < Z < a < b ... < y < z
字符串的子串是字符串中连续的字符块。例如,abc 的子串是 a、b、c、ab、bc 和 abc。
给定一个字符串s和一个整数k,完成函数,以便它找到长度为k的字典上最小和 最大的子字符串。
这是我的(不完全工作)解决方案:
我的代码
import java.util.*;
public class stringCompare {
public static String getSmallestAndLargest(String s, int k) {
String smallest, largest, temp;
/* Initially, define the smallest and largest substrings as the first k chars */
smallest = s.substring(0, k);
largest = s.substring(0, k);
for (int i = 0; i <= s.length() - k; i++) {
temp = s.substring(i, i + k);
for (int j = 0; j < k; j++) {
/* Check if the first char of the next substring is greater than the largest ones' */
if (temp.charAt(j) > largest.charAt(j)) {
largest = s.substring(i, i + k);
break;
}
/* Check if the first char of the next substring is less than the smallest ones' */
else if (temp.charAt(j) < smallest.charAt(j)) {
smallest = s.substring(i, i + k);
break;
}
/* Check if the first char of the next substring is either equal to smallest or largest substrings' */
else if (temp.charAt(j) == smallest.charAt(j)
|| temp.charAt(j) == largest.charAt(j)) {
// If so, move to the next char till it becomes different
}
/* If the first of char of the next substring is neither of these (between smallest and largest ones')
skip that substring */
else {
break;
}
}
}
return smallest + "\n" + largest;
}
public static void main(String[] args) {
String s;
int k;
try (Scanner scan = new Scanner(System.in)) {
s = scan.next();
k = scan.nextInt();
}
System.out.println(getSmallestAndLargest(s, k));
}
}
根据 HackerRank,此代码在 6 个案例中有 2 个失败。一种如下:
ASDFHDSFHsdlfhsdlfLDFHSDLFHsdlfhsdlhkfsdlfLHDFLSDKFHsdfhsdlkfhsdlfhsLFDLSFHSDLFHsdkfhsdkfhsdkfhsdfhsdfjeaDFHSDLFHDFlajfsdlfhsdlfhDSLFHSDLFHdlfhs
30
预期的输出是:
ASDFHDSFHsdlfhsdlfLDFHSDLFHsdl
sdlkfhsdlfhsLFDLSFHSDLFHsdkfhs
但我的变成:
DFHSDLFHDFlajfsdlfhsdlfhDSLFHS
sdlkfhsdlfhsLFDLSFHSDLFHsdkfhs
在调试模式下,我发现最小的子字符串在第 67 次迭代 (i) 之前都是正确的。我不知道为什么它在那一步变成了错误的,但确实如此。
有人可以帮我吗?
谢谢!