简体   繁体   English

为什么“最小”不返回任何东西?

[英]Why does “smallest” not return anything?

Hello so I am currently going through the Java challenges on HackerRank and ran into an issue I can't figure out with my code. 您好,所以我目前正在HackerRank上进行Java挑战,遇到一个我无法弄清楚我的代码的问题。

The prompt goes as follow, 提示如下:

Given a string, s, and an integer, k, complete the function so that it finds the lexicographically smallest and largest substrings of length k. 给定一个字符串s和一个整数k,完成函数,以便找到字典上长度为k的最小和最大子字符串。

Sample input: 输入样例:
welcometojava welcometojava
3 3

Sample output: 样本输出:
ava AVA
wel WEL

The smallest and largest substrings are solely based on the first integer. 最小和最大子字符串仅基于第一个整数。 So for my solution, I decided to create an integer array and populate it with the integer value of each character available in String s. 因此,对于我的解决方案,我决定创建一个整数数组,并使用String中可用的每个字符的整数值填充该数组。

I figured once I sorted the array, index[0] would equal the first letter of smallest while index[s.length()-1] would equal the first letter of largest. 我确定对数组进行排序后,index [0]等于最小的第一个字母,而index [s.length()-1]等于最大的第一个字母。 From there it would only be a matter of concatenating each following character within the array to the corresponding smallest/largest as long as smallest/largest <= k. 从那里开始,只要将最小/最大<= k,就可以将数组中的每个后续字符连接到相应的最小/最大。

With the sample input above, I am able to return "wel" for largest. 使用上面的示例输入,我可以将“ wel”返回最大。 smallest however is returning an empty string and I can't figure out why. 但是最小的返回一个空字符串,我不知道为什么。 I literally followed the same methodology to get the value of both strings so I think smallest should be returning a string value, regardless of whether said value is the right answer. 我从字面上遵循相同的方法来获取两个字符串的值,因此我认为最小的应该返回一个字符串值,而不管所说的值是否正确。

Here is my source code right below. 这是我下面的源代码。

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

    int[] temp = new int[s.length()];

    for (int i = 0; i < s.length(); i++) {
        temp[i] = (int) s.charAt(i);
    }

    Arrays.sort(temp);

    char[] charArray = s.toCharArray();

    // find smallest string
    for (int i = 0; i < charArray.length; i++) {

        if ((int) s.charAt(i) == temp[0]) {

            while (i < k) {
                smallest += String.valueOf(s.charAt(i));
                i++;
            }
        }
    }

    // find largest string
    for (int i = 0; i < charArray.length; i++) {

        if ((int) s.charAt(i) == temp[s.length()-1]) {

            while (i < k) {
                largest += String.valueOf(s.charAt(i));
                i++;
            }
        }
    }

    return smallest + "\n" + largest;
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String s = scan.next();
    int k = scan.nextInt();
    scan.close();

    System.out.println(getSmallestAndLargest(s, k));
}

I figured once I sorted the array, index[0] would equal the first letter of smallest while index[s.length() - 1] would equal the first letter of largest. 我确定对数组进行排序后, index[0]等于最小的第一个字母,而index[s.length() - 1]等于最大的第一个字母。

Well yes ... but that is not sufficient. 好吧……但是那还不够。 The problem is smallest letter can appear more than once in the original string. 问题是最小的字母可以在原始字符串中出现多次。 And so can the largest letter. 最大的字母也可以。 So when you find an instance of (say) 'a' in the original string, you don't know if it is the start of the smallest 3-character substring, or not. 因此,当您在原始字符串中找到(例如) 'a'的实例时,您不知道它是否是最小的3个字符的子字符串的开头。

There is a simpler approach. 有一种更简单的方法。

Hint: if you have an array of 3 character strings, how do you find the smallest and largest? 提示:如果您有3个字符串组成的数组,如何找到最小和最大的字符串?

Based on what you have, I think you really need to just map the characters in such a way that you order the leading character, then return the first and last ordered characters. 根据您拥有的内容,我认为您确实需要映射字符,以便您对前导字符进行排序,然后返回第一个和最后一个排序的字符。 Below is a modification of your getSmallestAndLargest method. 下面是对getSmallestAndLargest方法的修改。

public static String getSmallestAndLargest(String s, int k) {
        List<String> lexList = new ArrayList<String>();
        StringBuilder word;

        if(s.length() < k) {
            return s;
        }

        for(int i = 0; i < ((s.length() + 1) - k); i++) {
            int j = i;
            word = new StringBuilder();

            while (j < (i + k)) {
                word.append(s.charAt(j));
                j++;
            }

            lexList.add(word.toString());
        }

        Collections.sort(lexList);

        return lexList.get(0) + "\n" + lexList.get(lexList.size()-1);
}

For the line: (int) s.charAt(i) == temp[i], do you mean temp[0] instead of temp[i]? 对于以下行:(int)s.charAt(i)== temp [i],您是说temp [0]而不是temp [i]? The if statement will only be true when one of the two strings, 'welcometojava' and 'aaceejlmootvw', have the same letter at the same index, which doesn't happen. 只有当两个字符串“ welcometojava”和“ aaceejlmootvw”之一在相同的索引处具有相同的字母,而不会发生时,if语句才为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM