-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path290. Word Pattern.c
77 lines (58 loc) · 1.92 KB
/
290. Word Pattern.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
290. Word Pattern
Given a pattern and a string str, find if str 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 str.
Examples:
pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Credits:Special thanks to @minglotus6 for adding this problem and creating all test cases.
*/
bool wordPattern(char* pattern, char* str) {
char *bucket[26] = { 0 };
int len [26] = { 0 };
char c, *p, *s;
char t, *pat;
int l;
pat = pattern;
while (*str && *pattern) {
s = str;
l = 0;
while (*str && *str != ' ') {
str ++;
}
l = str - s;
if (*str == ' ') {
*str = 0; // strcmp is much faster than strncmp, so cut the strings.
str ++; // skip single space
}
c = *(pattern ++) - 'a';
p = bucket[c];
if (p) {
if (strcmp(p, s)) return false;
} else {
bucket[c] = s;
len[c] = l;
// cannot be same with other pattern
p = pat;
while ((t = (*p ++) - 'a') != c) {
if (len[t] == len[c] &&
!strcmp(bucket[t], bucket[c])) return false;
}
}
}
return (!*pattern && !*str) ? true : false;
}
/*
Difficulty:Easy
Total Accepted:82.1K
Total Submissions:248.7K
Companies Dropbox Uber
Related Topics Hash Table
Similar Questions
Isomorphic Strings
Word Pattern II
*/