Skip to content

Commit c7245f7

Browse files
committed
LEETCODE - 2023
1 parent 5006205 commit c7245f7

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

WordPattern/word_pattern.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 🔥 Word Pattern 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
import 'dart:collection';
7+
8+
class Solution {
9+
bool wordPattern(String pattern, String s) {
10+
// s is the sentence so we breaking it into each and every single word
11+
List<String> words = s.split('');
12+
// Two HashMap
13+
if (words.length != pattern.length) return false;
14+
15+
HashMap<int, String> firstHashMap = HashMap();
16+
HashMap<String, bool> secondHashMap = HashMap();
17+
18+
// looping through each and every character
19+
for (int i = 0; i < pattern.length; i++) {
20+
// getting the char code of every pattern
21+
int char = pattern.codeUnitAt(i);
22+
// because it has sentence so we taking the each character and making into a a single word
23+
// to compare with the char
24+
if (firstHashMap.containsKey(char) == false) {
25+
if (secondHashMap.containsKey(words[i]) == true) {
26+
return false;
27+
} else {
28+
secondHashMap[words[i]] = true;
29+
firstHashMap[char] = words[i];
30+
}
31+
} else {
32+
String mWith = firstHashMap[char]!;
33+
if (mWith.allMatches(words[i]) == false) {
34+
return false;
35+
}
36+
}
37+
}
38+
return true;
39+
}
40+
}
41+
```
42+
43+
## Solution - 2
44+
45+
```dart
46+
import 'dart:collection';
47+
48+
class Solution {
49+
bool wordPattern(String pattern, String s) {
50+
// initializing HashMap because it's fast
51+
final HashMap<String, String> map = HashMap();
52+
// splitting the sentence into each individual character
53+
final List<String> words = s.split(' ');
54+
final List<String> patternWords = pattern.split('');
55+
// if the length is not same
56+
if (words.length != patternWords.length) return false;
57+
// looping through each and every individual pattern word
58+
for (int i = 0; i < patternWords.length; i++) {
59+
// if the key is null or empty
60+
if (map[patternWords[i]] == null) {
61+
// but value contain it
62+
if (map.containsValue(words[i])) {
63+
return false;
64+
}
65+
// assign each and every character and word
66+
map[patternWords[i]] = words[i];
67+
} else {
68+
// if they are nt assign than false
69+
if (map[patternWords[i]] != words[i]) {
70+
return false;
71+
}
72+
}
73+
}
74+
return true;
75+
}
76+
}
77+
```
78+
79+
## BONUS - GOLANG
80+
81+
```go
82+
func wordPattern(pattern string, s string) bool {
83+
words := strings.Split(s, " ")
84+
if len(words) != len(pattern) {
85+
return false
86+
}
87+
hashMap := make(map[interface{}]int)
88+
for i, word := range words {
89+
if hashMap[word] != hashMap[pattern[i]] {
90+
return false
91+
}
92+
hashMap[word] = i + 1
93+
hashMap[pattern[i]] = i + 1
94+
}
95+
return true
96+
}
97+
```

0 commit comments

Comments
 (0)