Skip to content

Commit a0a466c

Browse files
Leetcode Problems 242, 290, 345, 383 Done.
1 parent 31864d0 commit a0a466c

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

3 Strings/242 Valid Anagram.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.HashMap;
2+
3+
class Solution {
4+
public boolean isAnagram(String s, String t) {
5+
if (s.length() != t.length()) {
6+
return false;
7+
}
8+
HashMap<Character, Integer> countMap = new HashMap<>();
9+
for (int i = 0 ; i < s.length(); i++) {
10+
char sChar = s.charAt(i);
11+
char tChar = t.charAt(i);
12+
13+
countMap.put(sChar, countMap.getOrDefault(sChar, 0) + 1);
14+
countMap.put(tChar, countMap.getOrDefault(tChar, 0) - 1);
15+
}
16+
17+
for (int value : countMap.values()) {
18+
if (value != 0) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
}

3 Strings/290 Word Pattern.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.HashMap;
2+
3+
class Solution {
4+
public boolean wordPattern(String pattern, String s) {
5+
String[] words = s.split(" ");
6+
if (pattern.length() != words.length) {
7+
return false;
8+
}
9+
HashMap<Character, String> lToW = new HashMap<>();
10+
HashMap<String, Character> wToL = new HashMap<>();
11+
for (int i = 0 ; i < pattern.length(); i++) {
12+
char letter = pattern.charAt(i);
13+
String word = words[i];
14+
15+
if (lToW.containsKey(letter)) {
16+
if (!lToW.get(letter).equals(word)) {
17+
return false;
18+
}
19+
} else {
20+
lToW.put(letter, word);
21+
}
22+
23+
if (wToL.containsKey(word)) {
24+
if (wToL.get(word) != letter) {
25+
return false;
26+
}
27+
} else {
28+
wToL.put(word, letter);
29+
}
30+
}
31+
return true;
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public String reverseVowels(String s) {
3+
char[] letters = s.toCharArray();
4+
int i = 0, j = s.length() - 1;
5+
6+
while (i < j) {
7+
// increment till i reaches a vowel
8+
while (i < j && !isVowel(letters[i])) i++;
9+
10+
// decrement till j reaches a vowel
11+
while (i < j && !isVowel(letters[j])) j--;
12+
13+
if (i < j) {
14+
char temp = letters[i];
15+
letters[i] = letters[j];
16+
letters[j] = temp;
17+
i++;
18+
j--;
19+
}
20+
}
21+
return new String(letters);
22+
}
23+
24+
private boolean isVowel(char c) {
25+
final String VOWELS = "aeiouAEIOU";
26+
return VOWELS.indexOf(c) != -1;
27+
}
28+
}

3 Strings/383 Ransom Note.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 canConstruct(String ransomNote, String magazine) {
3+
int[] counts = new int[26];
4+
for (char c : magazine.toCharArray()) {
5+
int index = c - 'a';
6+
counts[index]++;
7+
}
8+
for (char c : ransomNote.toCharArray()) {
9+
int index = c - 'a';
10+
counts[index]--;
11+
}
12+
for (int count: counts) {
13+
if (count < 0) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
}

0 commit comments

Comments
 (0)