public class Solution {
public bool WordPattern(string pattern, string str) {
var list = str.Split(' ').ToList();
int type1 = 0;
int type2 = 0;
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
Dictionary<string, int> dic1 = new Dictionary<string, int>();
Dictionary<char, int> dic2 = new Dictionary<char, int>();
foreach (var word in list)
{
if (!dic1.ContainsKey(word))
{
dic1.Add(word, type1);
sb1.Append(type1);
type1++;
}
else
{
sb1.Append(dic1[word]);
}
}
foreach (var c in pattern)
{
if (!dic2.ContainsKey(c))
{
dic2.Add(c, type2);
sb2.Append(type2);
type2++;
}
else
{
sb2.Append(dic2[c]);
}
}
if (sb1.ToString() != sb2.ToString())
{
return false;
}
else
{
return true;
}
}
}
https://leetcode.com/problems/word-pattern/#/description