-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0290-WordPattern.cs
43 lines (38 loc) · 1.23 KB
/
0290-WordPattern.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//-----------------------------------------------------------------------------
// Runtime: 68ms
// Memory Usage: 21.9 MB
// Link: https://leetcode.com/submissions/detail/358739161/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0290_WordPattern
{
public bool WordPattern(string pattern, string str)
{
var words = str.Split();
if (words.Length != pattern.Length) return false;
var map1 = new Dictionary<char, string>();
var map2 = new Dictionary<string, char>();
for (int i = 0; i < pattern.Length; i++)
{
var ch = pattern[i];
if (map1.ContainsKey(ch))
{
if (map1[ch] != words[i])
return false;
}
else
map1[ch] = words[i];
if (map2.ContainsKey(words[i]))
{
if (map2[words[i]] != ch)
return false;
}
else
map2[words[i]] = ch;
}
return true;
}
}
}