-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
40 lines (36 loc) · 1.21 KB
/
Solution.cs
File metadata and controls
40 lines (36 loc) · 1.21 KB
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
namespace LeetCode.Problem290{
//290. Word Pattern
//https://leetcode.com/problems/word-pattern/
/*
Given a pattern and a string s, find if s follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.
*/
public class Solution {
public bool WordPattern(string pattern, string s) {
Dictionary<char,int> dic = new Dictionary<char,int>();
Dictionary<int,string> words = new Dictionary<int,string>();
var tempWords = s.Split(' ');
if (tempWords.Length != pattern.Length)
return false;
for (int index = 0; index < pattern.Length; index++)
{
var value = pattern[index];
if (dic.ContainsKey(value))
{
int place = dic[value];
if (tempWords[index] != words[place])
return false;
dic[value] = index;
}
else
{
dic.Add(value,index);
if (words.ContainsValue(tempWords[index]))
return false;
}
words.Add(index,tempWords[index]);
}
return true;
}
}
}