打印给定大小 k 的字符串 s 中字典顺序最小和最大的子字符串。

huangapple go评论86阅读模式
英文:

print the lexicographically smallest and largest substring of a given size k from a string s

问题

以下是翻译好的内容:

这是一个用于打印长度为 k 的字典序最小和最大子字符串的程序。

这个解决方案中有一个部分我真的不太理解。也许有人可以解释一下给我。

public static String getSmallestAndLargest(String s, int k) {
    String smallest = "";
    String largest = "";
    String currStr = s.substring(0, k);

这是我不理解的部分。为什么 int i 被初始化为 k,以及以下代码是如何工作的:

for (int i = k; i < s.length(); i++) {
    currStr = currStr.substring(1, k) + s.charAt(i);

完整的循环:

for (int i = k; i < s.length(); i++) {
    currStr = currStr.substring(1, k) + s.charAt(i);
    if (lexMax.compareTo(currStr) < 0)
        lexMax = currStr;
    if (lexMin.compareTo(currStr) > 0)
        lexMin = currStr;
}
return smallest + "\n" + largest;
}
英文:

Its a program to print the Lexicographically smallest and largest substring of size k.

There´s a part in this solution I don´t really understand. Maybe someone can explain it to me.

 public static String getSmallestAndLargest(String s, int k) {
    String smallest = &quot;&quot;;
    String largest = &quot;&quot;;
    String currStr = s.substring(0, k); 

Here is the part I dont get. why is int i initialised with k and how does

for(int i = k; i&lt;s.length(); i++){
    currStr = currStr.substring(1, k) + s.charAt(i);

exactly work?


Full loop:

    for(int i = k; i&lt;s.length(); i++){
        currStr = currStr.substring(1, k) + s.charAt(i); 
        if (lexMax.compareTo(currStr) &lt; 0)      
             lexMax = currStr; 
        if (lexMin.compareTo(currStr) &gt; 0) 
             lexMin = currStr;             
    }       
    return smallest + &quot;\n&quot; + largest;
}

答案1

得分: 0

算法的思想是 currStr 遍历所有长度为 k 的子字符串。它从索引 0 到索引 k-1 的子字符串开始:

String currStr = s.substring(0, k); // 结束索引被排除在外!

然后要获取下一个子字符串,从索引 1 到 k:

  • 首先,它去掉了 currStr 的第一个字符,这样你就得到了一个从索引 1 到索引 k-1 的子字符串。

  • 然后,它添加了输入字符串索引 k 处的字符。最终结果是从索引 1 到索引 k 的子字符串。

然后,这个过程会重复,以获取从索引 2 到 k+1 的子字符串以及所有后续的子字符串。

currStr = currStr.substring(1, k) // 从 currStr 中去掉第一个字符
             + s.charAt(i);       // 添加输入字符串的下一个字符
英文:

The idea of the algorithm that currStr goes through all substrings of length k. It starts with substring from index 0 to index k-1:

String currStr = s.substring(0, k); // end index is excluded!

Then to get the next substring, the one from index 1 to k:

  • First it drops the first character from currStr, so you get a substring from index 1 to index k-1.

  • Then it adds the character from index k from the input string. The end result is the substring from index 1 to index k.

This process is then repeated to get the substring from index 2 to k+1 and all the following substrings.

currStr = currStr.substring(1, k) // drop the first character from currStr
             + s.charAt(i);       // add next character from input string

huangapple
  • 本文由 发表于 2020年4月8日 20:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61101148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定