DunderMifflin11
DunderMifflin11

Reputation: 29

How would I check to see if two strings contain the same letters

I'm currently completing a programming challenge in C#, I'm stuck on the main part. The application must take two words and see if they contain the same letters. How would I go about checking to see if input1 and input2 contain the same letters?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Words_With_Enemies
{
    class Program
    {

        static string input1, input2;

        public void findLetters()
        {
            bool regexWord1 = Regex.IsMatch(input1, @"^[a-zA-Z]+$");
        }

        static void Main(string[] args)
        {

            Console.WriteLine("Please enter two words");
            input1 = Console.ReadLine();
            input2 = Console.ReadLine();

            Console.WriteLine("You have entered the following two words:");
            Console.WriteLine(input1);
            Console.WriteLine(input2);

            Console.ReadLine();

        }

     }
}

Upvotes: 2

Views: 3604

Answers (2)

Farhad Jabiyev
Farhad Jabiyev

Reputation: 26635

If you want to find if all letters are same in both strings, then you can use Except() from System.Linq namespace:

bool result = input1.Except(input2).Any();

It will return true if they don't contain same letters.

The output for such inputs will be like that:

Apples, Apple => True
Apples, Banana => True
Apple, Alep => False
Apple, Apple => False

Update:

If you want to find if any letter is contained in both strings, then you can use Intersect():

bool result = input1.Intersect(input2).Any();

It will return true if they contain at least one same letter. The output for such inputs will be like that:

The output for such inputs will be like that:

Apples, Apple => True
Apples, Banana => True
Apple, Alep => True
Apple, Onion => False


Additional detail:
If you want to find the result case-insensitively, then you can change both codes as:

bool result = input1.ToLowerInvariant().Except(input2.ToLowerInvariant()).Any();
bool result = input1.ToLowerInvariant().Intersect(input2.ToLowerInvariant()).Any();

Upvotes: 8

Matan Perry
Matan Perry

Reputation: 67

basically you want to check if two strings are permutations.

    static private bool isPermutation(string myString1, string myString2)
    {
        //If the strings are different lengths, they are not
        //permutations.
        if (myString1.Length != myString2.Length) return false;

        //Create an array to count the number of each specific
        //character in the strings.
        int[] characterCount = new int[256];
        int charIndex;

        //Populate the array with default value 0.
        for (int index = 0; index < 256; index++)
        {
            characterCount[index] = 0;
        }

        //Count the number of each character in the first
        //string. Add the count to the array.
        foreach (char myChar in myString1.ToCharArray())
        {
            charIndex = (int)myChar;
            characterCount[charIndex]++;
        }

        //Count the number of each character in the second
        //string. Subtract the count from the array.
        foreach (char myChar in myString2.ToCharArray())
        {
            charIndex = (int)myChar;
            characterCount[charIndex]--;
        }

        //If the strings are permutations, then each character
        //would be added to our character count array and then
        //subtracted. If all values in this array are not 0
        //then the strings are not permutations of each other.
        for (int index = 0; index < 256; index++)
        {
            if (characterCount[index] != 0) return false;
        }

        //The strings are permutations of each other.
        return true;
    }
}
}

Upvotes: 0

Related Questions