Skip to content

Commit 09136af

Browse files
committed
leetcode
1 parent 4a7b1c7 commit 09136af

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
8585
- [Implement Queue using Stacks](ImplementQueueUsingStacks/implement_queue_using_stacks.dart)
8686
- [3Sum Closest](3SumClosest/3_sum_closest.dart)
8787
- [Two Sum IV - Input is a BST](TwoSumIV-InputIsABST/two_sum_IV_input_is_a_bst.dart)
88+
- [Valid Anagram](ValidAnagram/valid_anagram.dart)
8889

8990
## Reach me via
9091

ValidAnagram/valid_anagram.dart

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
3+
4+
-* Valid Anagram *-
5+
6+
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
7+
8+
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
9+
10+
11+
12+
Example 1:
13+
14+
Input: s = "anagram", t = "nagaram"
15+
Output: true
16+
Example 2:
17+
18+
Input: s = "rat", t = "car"
19+
Output: false
20+
21+
22+
Constraints:
23+
24+
1 <= s.length, t.length <= 5 * 104
25+
s and t consist of lowercase English letters.
26+
27+
28+
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
29+
30+
*/
31+
32+
class A {
33+
bool isAnagram(String s, String t) {
34+
if (s.length != t.length) return false;
35+
final List<String> listOne = s.split('')..sort();
36+
final List<String> listTwo = t.split('')..sort();
37+
38+
return listOne.join() == listTwo.join();
39+
}
40+
}
41+
42+
class B {
43+
bool isAnagram(String s, String t) {
44+
if (s.length != t.length) return false;
45+
final List<String> listOne = s.split('');
46+
final List<String> listTwo = t.split('');
47+
for (int i = 0; i < listOne.length; i++) {
48+
int index = listTwo.indexWhere((element) => element == listOne[i]);
49+
if (index >= 0) {
50+
listTwo.removeAt(index);
51+
} else {
52+
return false;
53+
}
54+
}
55+
return true;
56+
}
57+
}
58+
59+
class C {
60+
bool isAnagram(String s, String t) {
61+
if (s.length != t.length) return false;
62+
final Map<String, int> mapOne = {};
63+
final Map<String, int> mapTwo = {};
64+
65+
for (int i = 0; i < s.length; i++) {
66+
mapOne[s[i]] = (mapOne[s[i]] ?? 0) + 1;
67+
mapTwo[t[i]] = (mapTwo[t[i]] ?? 0) + 1;
68+
}
69+
final mapOneKeys = mapOne.keys.toList();
70+
final mapOneValues = mapOne.values.toList();
71+
72+
for (int i = 0; i < mapOne.length; i++) {
73+
final key = mapOneKeys[i];
74+
final value = mapOneValues[i];
75+
76+
if (mapTwo[key] != value) return false;
77+
}
78+
return true;
79+
}
80+
}

ValidAnagram/valid_anagram.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
/*
4+
5+
bool isAnagram(String s, String t) {
6+
if (s.length != t.length) return false;
7+
final Map<String, int> mapOne = {};
8+
final Map<String, int> mapTwo = {};
9+
10+
for (int i = 0; i < s.length; i++) {
11+
mapOne[s[i]] = (mapOne[s[i]] ?? 0) + 1;
12+
mapTwo[t[i]] = (mapTwo[t[i]] ?? 0) + 1;
13+
}
14+
final mapOneKeys = mapOne.keys.toList();
15+
final mapOneValues = mapOne.values.toList();
16+
17+
for (int i = 0; i < mapOne.length; i++) {
18+
final key = mapOneKeys[i];
19+
final value = mapOneValues[i];
20+
21+
if (mapTwo[key] != value) return false;
22+
}
23+
return true;
24+
}
25+
26+
*/
27+
func isAnagram(s string, t string) bool {
28+
sr := map[byte]int{}
29+
tr := map[byte]int{}
30+
31+
if len(s) != len(t) {
32+
return false
33+
}
34+
35+
for i := 0; i < len(s); i++ {
36+
sr[s[i]] += 1
37+
tr[t[i]] += 1
38+
}
39+
40+
for i := 0; i < len(s); i++ {
41+
if sr[s[i]] != tr[s[i]] {
42+
return false
43+
}
44+
}
45+
46+
return true
47+
}

ValidAnagram/valid_anagram.md

Whitespace-only changes.

0 commit comments

Comments
 (0)