I have 2 strings of pattern `a.{var1}.{var2}` and `b.{var1}.{var2}`. 

Two Strings are matching if `var1` in first string is same as `var1` in second string, as well as `var2` in first string is same as `var2` in second string. 

The variables can be any order like `a.{var1}.{var2}` and `b.{var2}.{var1}`. 

How do I match the two strings efficiently?

Example 1:

```
String pattern1 = "1.{var1}";
String pattern2 = "2.{var1}";

//Match True = (1.111,2.111)
//Match False = (1.121,2.111)

```

Example 2:

```
String pattern1 = "1.{var1}.{var2}";
String pattern2 = "2.{var1}.{var2}";

//Match True = (1.11.22,2.11.22)
//Match False = (1.11.22,2.111.22)

```

Example 3:

```
String pattern1 = "1.{var1}.{var2}";
String pattern2 = "2.{var2}.{var1}";

//Match True = (1.22.11,2.11.22)
//Match False = (1.11.22,2.111.22)

```

So whats the best way to match these 2 strings?

I want to match these 2 strings to find if they are related with the pattern mentioned.  

Extending this problem to a set of strings i.e Set A strings has to be matched with strings in Set B. Finally pairs of strings have to be formed which satisfy this matching algorithm. The pattern will remain the same when matching for all strings in Set A to Set B.