You must Sign In to post a response.
  • Category: .NET

    How to check between two common strings one vowel missing and one consonant is different

    how to check between two common strings one vowel missing and one consonant is different
    and rest of the letters are same.

    please help

    ex :- string1 : ABCDEFGH string2 : ABCDFGK
  • #770436
    Hi Chanti

    please try below code

    static void Main()
    {
    string string1 = "ABCDEFGH";
    string string2 = "ABCDFGK";

    bool result = CheckStrings(string1, string2);
    Console.WriteLine(result ? "Strings match the criteria." : "Strings do not match the criteria.");
    }

    static bool CheckStrings(string str1, string str2)
    {
    // Check if lengths match the required criteria
    if (Math.Abs(str1.Length - str2.Length) != 1)
    return false;

    // Identify missing vowel and different consonant
    int vowelCount1 = 0, vowelCount2 = 0;
    char differentChar = '\0';
    bool foundDifference = false;

    int minLength = Math.Min(str1.Length, str2.Length);

    for (int i = 0; i < minLength; i++)
    {
    if (str1[i] != str2[i])
    {
    // If we already found a difference, it's invalid
    if (foundDifference)
    return false;

    differentChar = str1[i] != str2[i] ? str2[i] : str1[i];
    foundDifference = true;
    }

    if (IsVowel(str1[i])) vowelCount1++;
    if (IsVowel(str2[i])) vowelCount2++;
    }

    // Handle the extra character in the longer string
    if (str1.Length > str2.Length)
    {
    if (IsVowel(str1[minLength])) vowelCount1++;
    }
    else
    {
    if (IsVowel(str2[minLength])) vowelCount2++;
    }

    // Check if we have one vowel missing and one consonant different
    return (Math.Abs(vowelCount1 - vowelCount2) == 1) && foundDifference;
    }

    static bool IsVowel(char c)
    {
    return "AEIOUaeiou".IndexOf(c) >= 0;
    }

  • #770437
    u can try this
    class Program
    {
    static void Main()
    {
    string string1 = "ABCDEFGH";
    string string2 = "ABCDFGK";

    bool result = CheckStrings(string1, string2);

    Console.WriteLine(result ? "Strings match the criteria" : "Strings do not match the criteria");
    }

    static bool CheckStrings(string s1, string s2)
    {
    // Define vowels and consonants
    char[] vowels = { 'A', 'E', 'I', 'O', 'U' };
    char[] consonants = "BCDFGHJKLMNPQRSTVWXYZ".ToCharArray();

    // Check length difference
    if (Math.Abs(s1.Length - s2.Length) != 1)
    {
    return false; // One should be one letter longer due to the missing vowel
    }

    // Count consonant differences
    int consonantDifferences = 0;
    for (int i = 0, j = 0; i < s1.Length && j < s2.Length; i++, j++)
    {
    if (s1[i] != s2[j])
    {
    if (consonants.Contains(s1[i]) && consonants.Contains(s2[j]))
    {
    consonantDifferences++;
    if (consonantDifferences > 1)
    {
    return false;
    }
    }
    else if (vowels.Contains(s1[i]))
    {
    j--; // s2 is missing a vowel, so decrement j to re-check this position
    }
    else if (vowels.Contains(s2[j]))
    {
    i--; // s1 is missing a vowel, so decrement i to re-check this position
    }
    else
    {
    return false; // Characters don't match and it's not a vowel
    }
    }
    }

    // Ensure one consonant difference and one missing vowel
    return consonantDifferences == 1 && (s1.Length > s2.Length ? CountVowels(s1) - CountVowels(s2) == 1 : CountVowels(s2) - CountVowels(s1) == 1);
    }

    static int CountVowels(string s)
    {
    char[] vowels = { 'A', 'E', 'I', 'O', 'U' };
    return s.Count(c => vowels.Contains(c));
    }
    }


  • Sign In to post your comments