hackerrank-java子串比较错误

时间:2017-10-09 01:03:43

标签: java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static String getSmallestAndLargest(String s, int k) {
        String smallest = s.substring(0,k);
        String largest = s.substring(0,k);
        for(int i=0;i<s.length()-k;i++){


            String curr = s.substring(i, i + k);
            if (smallest.compareTo(curr) > 0){
                smallest = curr;
            }
            if (largest.compareTo(curr) < 0) {
                largest = curr;
            }
        }


        return smallest + "\n" + largest;
    }

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

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

代码应返回按字典顺序排列的最小和最大的子字符串,但看起来我在某个无法识别的地方出错了。

输入(stdin):

welcometojava
3

您的输出(标准输出):

com
wel

预期产出:

ava
wel

3 个答案:

答案 0 :(得分:1)

尝试此操作以获取最后3个字符串:

c

答案 1 :(得分:0)

这是因为这个循环:

for(int i=0;i<s.length()-k;i++){

看不到最后三个字母 - 你停在j,而不是a。您可以通过调试或考虑终止条件来找到它。你需要:

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

答案 2 :(得分:0)

关注此代码

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

        char[] strArr = s.toCharArray();
        int size = strArr.length;

        char smallestChar = smallest_alphabet(strArr, size);
        char largestChar  = largest_alphabet(strArr, size);

        String[] smallestCharStringSet =  prepareSetOfString(s, k, smallestChar);
        String[] largestCharStringSet = prepareSetOfString(s, k, largestChar);

        smallest = smallestString(smallestCharStringSet);
        largest = largestString(largestCharStringSet);
        return smallest + "\n" + largest;
    }

    public static String[] prepareSetOfString(String s, int k, char c){
        int count = getCharCount(s,c);

        String[] possibleStringCollection  = new String[count];
        String operationString = s;
        possibleStringCollection[0] = operationString;
        for(int i=0; i< count; i++){
            int indexOfChar = operationString.indexOf(c+"");
            if(indexOfChar+k <= operationString.length()){
                possibleStringCollection[i] = operationString.substring(indexOfChar, indexOfChar+k);
                    operationString = operationString.substring(operationString.indexOf(c+"")+1);
            }
        }

        return possibleStringCollection;
    }

    public static String smallestString(String[] smallestCharStringSet){
        String smallestString = smallestCharStringSet[0];
        for(int i = 0; i < smallestCharStringSet.length; i++){
            if(smallestCharStringSet[i] != null)
                if(smallestString.compareTo(smallestCharStringSet[i]) > 0)
                {
                    smallestString = smallestCharStringSet[i];
                }
        }
        return smallestString;
    }

    public static String largestString(String[] largestCharStringSet){
        String largestString = largestCharStringSet[0];
        for(int i = 0; i < largestCharStringSet.length; i++){
            if(largestCharStringSet[i] != null)
                if(largestString.compareTo(largestCharStringSet[i]) < 0)
                {
                    largestString = largestCharStringSet[i];
                }
        }
        return largestString;
    }

    public static int getCharCount(String s, char c)
    {
        int count = 0;
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) == c)
                count++;
        }
        return count;
    }

    public static char largest_alphabet(char a[], int n)
    {
        char max = 'A';

        for (int i=0; i<n; i++)
            if (a[i] > max)
                max = a[i];

        return max;
    }

    public static char smallest_alphabet(char a[], int n)
    {
        char min = 'z';

        for (int i=0; i<n-1; i++)
            if (a[i] < min)
                min = a[i];

        return min;
    }