Strings: Making Anagrams

  • + 0 comments

    Here is simple C# code, that does the trick. Here I am deleting characters thats are found from both the strings (1 character each). We will be left with characters that are now found in both.

    public static int makeAnagram(string a, string b) { string sS1 = ""; string sS2 = "";

        if(a.Length > b.Length)
        {
            sS1 = a;
            sS2 = b;
        }
        else
        {
            sS1 = b;
            sS2 = a;
        }
    
        for(int i = 0; i < sS1.Length; i++)
        {
            for(int j = 0; j < sS2.Length; j++)
            {
                if(sS1[i] == sS2[j])
                {
                    sS1 = sS1.Remove(i,1);
                    sS2 = sS2.Remove(j,1);
                    i--;
                    j--;
                    break;
                }
            }
            continue;
        }
    
        return sS1.Length + sS2.Length;
    
    
    }