如何计算可能缺少的字符?

时间:2017-10-05 09:08:29

标签: java string loops for-loop char

我正试图找到可能缺少的字符,例如:

输入 - > aa??bb应该有可能的字符aaaabb& aaabbb& aabbbb所以结果是3,?a?也是1。

注意: aababb会出错,因为它不是字母表的正确途径。

我在这里做了一些代码,但我还是得不到完美的结果。

有人可以帮帮我吗?

Scanner input = new Scanner(System.in);
    String s = input.nextLine();
    int possibleAlphabet = 1, oldPossibleAlphabet = 0;
    for (int i = 0; i < s.length(); i++) {
        oldPossibleAlphabet = 0;
        System.out.print(s.charAt(i));
        if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z'  || s.contains("?")) {
            if (s.charAt(i) == '?'){
                for (int j = 0; j < i; j++) {
                    if (s.charAt(i - 1) == '?' && s.charAt(i + 1) == '?')
                        oldPossibleAlphabet++;

                }
            }
        }else {
            System.out.print( " ");
            System.exit(0);
        }
        possibleAlphabet += oldPossibleAlphabet;
    }
    System.out.println(possibleAlphabet);

1 个答案:

答案 0 :(得分:0)

检查我的代码

public class Solution {

    public static void main(String[] args) {
        String str = "abc??cde???g?"; // Example
        char[] arr = str.toCharArray();
        int length = arr.length;

        // Init value for count, start and end
        int count = 0;
        char start = 'a';
        char end = 'a';
        for (int i = 0; i < length; i++) {
            if (arr[i] == '?') { // We found a question mark
                boolean foundEnd = false;
                int total = 1; // Currently the total of question mark is 1
                for (int j = i + 1; j < length; j++) { // Count the total question mark for our method and the end character
                    if (arr[j] != '?') { // Not question mark
                       end = arr[j]; // Update end;
                       i = j -1;
                       foundEnd = true;
                       break;
                    } else {
                        total++;
                    }
                }

                if (!foundEnd) { // Change end to start in the case our question mark continue to the end of string
                    end = start;
                }
                // Start to counting and reset end to 'z'
                int result = countPossibleCharacters(total, start, end);
                if (count > 0) {
                    count *= result;
                } else {
                    count += result;
                }
                end = 'z';
            } else {
                start = arr[i];
            }
        }

        System.out.println("The total is : " + count);
    }

    /**
     * Count the possible characters
     * @param total the total question mark
     * @param start the character in the left side of question mark
     * @param end the character in the right side of question mark
     * @return
     */
    static int countPossibleCharacters(int total, char start, char end) {
        if (total == 0) {
            return 0;
        }

        if (total == 1) {
            return end - start + 1;
        }

        if (total >= 2) {
            int count = 0;

            /**
             * We have a range of characters from start to end
             * and for each character we have 2 options: use or don't use it
             */
            // We use it, so the total of question mark will be decrement by 1
            count += countPossibleCharacters(total - 1, start, end);

            // We don't use it, so the range of characters will be decrement by 1
            if (start < end) {
                count += countPossibleCharacters(total, ++start, end);
            }

            return count;
        }

        return 0;
    }
}

规则适用于我的代码

  1. 字符串中的所有字符均为小写

  2. 左侧的字符必须低于右侧的字符

  3. 如果我们在字符串的开头有一个问号,我们将从'a'循环到下一个非问号字符

  4. 如果我们在字符串的末尾有问号,我们会将其替换为之前的非问号字符