2017-09-22 3 views
1

J'ai essayé pendant plusieurs heures de trouver un moyen de déterminer si deux chaînes ont une seule lettre en commun (une seule) en utilisant la récursivité en C# ...C# - Comment déterminer si deux chaînes ont une seule lettre commune en utilisant la récursivité

Par exemple, si l'word1 était « bonjour » et le word2 était « au revoir » elle devrait retourner true parce qu'il n'y a qu'un seul « e » en commun. Cependant, si le word1 était « bonjour » et le word2 était « jaune » ou « banane », il devrait revenir false parce qu'il ya plus d'une lettre en commun entre « bonjour » et « jaune » et aucun dans « banane »

Voici ce que je l'ai fait jusqu'à présent, mais je ne comprends pas pourquoi il ne retourne pas le résultat escompté:

private static bool didHaveOneCaracterInCommon(string word1, string word2, int index) 
{ 
    int indexChar = 0; 
    if(index + 1 < word1.Length) 
     indexChar = word2.IndexOf(word1[index]); 
    if (indexCar != -1) //There is at least one char in common 
    { 
     //Verify if there is another one character in common 
     if ((index + 1 < word1.Length && didHaveOneCaracterInCommon(word1,word2.Remove(indexChar, 1), index + 1)) 
      return false; 
     return true; 
    } 

    if (index + 1 == word1.Length) 
     return false; 

    return didHaveOneCaracterInCommon(word1, word2, index + 1); 
} 

Merci à l'avance!

+0

Avez-vous d'utiliser récursion? Peut-être plus facile d'utiliser un ensemble ou un linq? – hellyale

+0

Oui je fais ... Je sais que cela aurait été plus facile sans cela, mais l'exercice nécessite de coder la fonction avec récursivité –

+0

J'ai mis à jour mon pseudo code avec du vrai code et l'ai testé – hellyale

Répondre

0

Je recommande approcher un peu différemment avec un cas de base plus claire

private static bool charInCommon(string word1, string word2, int index) 
    { 
     int indexChar = 0; 

      indexChar = word2.IndexOf(word1[index]); 

     if (indexChar != -1) 
     { 
      return true; 
     } 
     return false; 
    } 

    private static bool onlyOneCaracterInCommon(string word1, string word2, int index = 0, bool commonfound = false) 
    { 
     if (index >= word1.Length) { return commonfound; } 
     if (commonfound) //if you find another return false 
     { 
      if (charInCommon(word1, word2, index)) 
      { return false; } 
     } 
     else 
     { 
      if (charInCommon(word1, word2, index)) 
      { commonfound = true; } 
      return onlyOneCaracterInCommon(word1, word2, index + 1, commonfound); 
     }   
     return onlyOneCaracterInCommon(word1, word2, index + 1, commonfound); 
    } 

Edit: Changement de pseudo-code au code réel

Il y a deux basecases ici:

Edit: signature changé Donc vous pouvez l'appeler avec juste word1 et word2.

1) Atteindre la fin de la chaîne 2) Atteindre le second caractère commun aux deux chaînes.

+0

'onlyOneCaracterInCommon (" hello "," banana " , 100, true) '- retournera vrai – Fabio

1

vous pouvez approcher comme ça

public static bool ExclusiveCharInCommon(string l, string r) 
    { 
     int CharactersInCommon(string f, string s) 
     { 
      if (f.Length == 0) return 0; 
      return ((s.IndexOf(f[0]) != -1) ? 1 : 0) + CharactersInCommon(f.Substring(1), s); 
     } 
     return CharactersInCommon(l, r) == 1; 
    }