2017-10-05 4 views
0

J'essaie d'obtenir des caractères possibles manquants par exemple:Comment calculer les caractères manquants possibles?

entrée ->aa??bb il devrait y avoir des caractères possibles aaaabb & aaabbb & aabbbb de sorte que le résultat serait 3, également ?a? serait 1.

Remarque : aababb aurait tort, car ce n'est pas un bon chemin pour l'alphabet.

J'ai fait du code ici mais je n'ai pas encore trouvé le résultat parfait.

Quelqu'un peut-il m'aider?

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

@MuratK. J'ai oublié de mentionner cela, ce serait dans le même chemin, par exemple 'acb' est faux 'abc' est juste. – user1058652

+0

pourquoi 'aababb' n'est pas un cas possible? –

+0

@mustabelMo parce qu'un est venu avant b, comme un avant deux! – user1058652

Répondre

1

Vérifiez mon code

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; 
    } 
} 

Règles applicables dans mon code

  1. Tous les caractères de chaîne sont en minuscules

  2. caractère sur le côté gauche doit être inférieure que le personnage sur le côté droit

  3. Si nous avons un point d'interrogation au début de la chaîne, nous boucle de « a » au caractère suivant marque non question

  4. Si nous marquons des questions à la fin de la chaîne, nous » ll le remplacera par le caractère de non-interrogation précédent

+0

'? A? 'Le total est de 26? – user1058652

+0

Pour le premier point d'interrogation, nous avons seulement 1 caractère 'a' est costume avec lui . Dans le deuxième point d'interrogation, il doit s'agir d'un caractère -> z = 26 . Le total est de 1 * 26 = 26 –

+0

cela devrait être un cas seulement, pas 26 alors? – user1058652