2017-10-05 4 views
0
私は可能性不足している文字を取得しようとしている

は、例えば:欠けている可能性のある文字を計算するには?

入力 - >aa??bbので結果は、3になります。また?a?は1

注だろう可能な文字& aaabbb & aabbbbaaaabbがあるはずです: 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); 
+0

2 @ [email protected] MuratK私はそれが同じ経路にあることを忘れていました。例えば 'acb'が間違っています' abc'が正しいです。 – user1058652

+0

なぜ 'aababb'は可能なケースではありませんか? –

+0

@mustabelMoはbの前に来たので、2の前のように! – user1058652

答えて

1

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. 我々は文字列の先頭に疑問符を持っている場合は、我々は、文字列の末尾に疑問符を持っている場合は、我々は、我々は「」次の非疑問符の文字

  4. からループ」よこれを以前の非疑問符文字に置き換えます。

+0

'?a? 'を考慮する必要があります。合計は26ですか? – user1058652

+0

最初の疑問符は、「a」という文字が1文字しかありません。 とのスーツです。 2番目の疑問符では、a - > z = 26文字の でなければなりません。合計は1 * 26 = 26 –

+0

です。これは1つのケースのみで、26ではありません。 – user1058652

関連する問題