Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0013be3

Browse files
authored
Merge pull request #2 from reeshijoshi/main
Java Solution for 217, 242
2 parents fa593d0 + cd2429c commit 0013be3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

java/217-Contains-Duplicate.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean containsDuplicate(int[] nums) {
3+
Set<Integer> numbers = new HashSet<Integer>();
4+
for (int num : nums) {
5+
if (numbers.contains(num)) return true;
6+
numbers.add(num);
7+
}
8+
9+
return false;
10+
}
11+
}

java/242-Valid-Anagram.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
if (s.length() != t.length()) return false;
4+
5+
int[] countS = new int[26];
6+
int[] countT = new int[26];
7+
8+
for (int i = 0 ; i < s.length() ; i++) {
9+
countS[s.charAt(i) - 'a']++;
10+
countT[t.charAt(i) - 'a']++;
11+
}
12+
13+
for (int i = 0 ; i < 26 ; i++) {
14+
if (countS[i] - countT[i] != 0) return false;
15+
}
16+
17+
return true;
18+
}
19+
}

0 commit comments

Comments
 (0)