lundi 2 octobre 2023

Anagrame: 2 word with the sema letters but different of order

 Anagrame: 2 word with the sema letters but different of order


using System;

using System.Linq;

using System.IO;

using System.Text;

using System.Collections;

using System.Collections.Generic;

using System.Text.Json;

using System.Text.Json.Serialization;

using System.Runtime.Serialization;


public class Solution {


    /**

     * @param wordA The first word, contains only uppercase and lowercase letters.

     * @param wordB The second word, contains only uppercase and lowercase letters.

     * @return True if the two words are anagrams, false otherwise.

     */

    public static bool IsAnagram(string wordA, string wordB) {

        // Write your code here

        if(wordA.Length > 30 || wordB.Length > 30){

            throw new ArgumentException("string is too long");

        }

        

        wordA = wordA.ToLower();

        wordB = wordB.ToLower();


        char[] arrayA = new char[wordA.Length]; 

        for (int i = 0; i < wordA.Length; i++) {  

            arrayA[i] = wordA[i];  

        }  


        char[] arrayB = new char[wordB.Length]; 

        for (int j = 0; j < wordB.Length; j++) {  

            arrayB[j] = wordB[j];  

        }  


        if(arrayA.Length != arrayB.Length){

            return false;

        }


        Array.Sort(arrayA);

        Array.Sort(arrayB);


        if (arrayA.SequenceEqual(arrayB))

        {

            return true;

        }


        return false;

    }


    /* Ignore and do not change the code below */


    /**

     * Try a solution

     * @param output True if the two words are anagrams, false otherwise.

     */

    private static void TrySolution(bool output) {

        Console.WriteLine("" + JsonSerializer.Serialize(output));

    }


    public static void Main(string[] args) {

        TrySolution(IsAnagram(

            JsonSerializer.Deserialize<string>(Console.ReadLine()),

            JsonSerializer.Deserialize<string>(Console.ReadLine())

        ));

    }

    /* Ignore and do not change the code above */

}


============================================

solution 2:


public static bool IsAnagram(string wordA, string wordB) {
        // Write your code here
         if(wordA.Length > 30 || wordB.Length > 30){
            throw new ArgumentException("word is too long");
        }    
       
        if (wordA.Length != wordB.Length)
        {
            return false;
        }

        wordA = wordA.ToLower();
        wordB = wordB.ToLower();
        char[] arrayA = wordA.ToCharArray();
        char[] arrayB = wordB.ToCharArray();
       
        Array.Sort(arrayA);
        Array.Sort(arrayB);
       
        return arrayA.SequenceEqual(arrayB);
    }

Aucun commentaire:

Enregistrer un commentaire