Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,866 questions

51,788 answers

573 users

How to check if two strings have the same words in different order with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class Program
{
	public static bool two_strings_have_same_words_in_different_order(string str1, string str2) {
		if (str1.Length != str2.Length) {
            return false;
		}
        
        string[] arr1 = str1.Split(' ');
        List<string> wordsOfStr1 = new List<string>(arr1);
        
        string[] wordsOfStr2 = str2.Split(' ');
    
        for (int i = 0; i < wordsOfStr2.Length; i++) {
            if (!wordsOfStr1.Contains(wordsOfStr2[i])) {
                return false;
            }
        }

        return true;
	}

	public static void Main(string[] args)
	{
		string str1 = "java c# c c++ python";
		string str2 = "python c++ java c# c";

		if (two_strings_have_same_words_in_different_order(str1, str2)) {
			Console.WriteLine("yes");
		}
		else {
			Console.WriteLine("no");
		}
	}
}


 
 
/*
run:
 
yes

*/

 



answered May 9, 2024 by avibootz
...