Skip to content

Commit 11dacb6

Browse files
authored
Merge pull request neetcode-gh#1443 from tharunkanna14/main
Create 1189-Maximum-Number-of-Balloons, Create 724-Find-Pivot-Index, Create 290-Word-Pattern
2 parents 434dce6 + f56d1ba commit 11dacb6

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

java/1189-Maximum-Number-of-Balloons

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int maxNumberOfBalloons(String text) {
3+
HashMap<Character, Integer> balloon = new HashMap<>();
4+
HashMap<Character, Integer> countText = new HashMap<>();
5+
6+
char[] balloonArray = "balloon".toCharArray();
7+
8+
for (char c : balloonArray) {
9+
if (balloon.containsKey(c)) {
10+
balloon.put(c,balloon.get(c)+1);
11+
} else {
12+
balloon.put(c,1);
13+
}
14+
}
15+
16+
char[] countTextArray = text.toCharArray();
17+
18+
for (char c : countTextArray) {
19+
if (countText.containsKey(c)) {
20+
countText.put(c,countText.get(c)+1);
21+
} else {
22+
countText.put(c,1);
23+
}
24+
}
25+
26+
int res = text.length();
27+
for (Character c : balloon.keySet()) {
28+
res = Math.min(res,countText.getOrDefault(c,0) / balloon.get(c));
29+
}
30+
31+
return res;
32+
}
33+
}

java/290-word-pattern

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public boolean wordPattern(String pattern, String s) {
3+
String[] sArray = s.split("\s");
4+
if(sArray.length != pattern.length()) {
5+
return false;
6+
}
7+
8+
HashMap<Character,String> charToWord = new HashMap<>();
9+
HashMap<String,Character> wordToChar = new HashMap<>();
10+
11+
for (int i = 0; i < pattern.length(); i++) {
12+
13+
if(charToWord.containsKey(pattern.charAt(i)) && !charToWord.get(pattern.charAt(i)).equals(sArray[i])) {
14+
return false;
15+
}
16+
17+
if(wordToChar.containsKey(sArray[i]) && !wordToChar.get(sArray[i]).equals(pattern.charAt(i))) {
18+
return false;
19+
}
20+
21+
charToWord.put(pattern.charAt(i),sArray[i]);
22+
wordToChar.put(sArray[i],pattern.charAt(i));
23+
}
24+
return true;
25+
}
26+
}

java/724-Find-Pivot-Index

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int pivotIndex(int[] nums) {
3+
int totalSum = 0;
4+
for (int i = 0; i < nums.length; i++) {
5+
totalSum += nums[i];
6+
}
7+
int leftSum = 0;
8+
for (int i = 0; i < nums.length; i++) {
9+
int rightSum = totalSum - leftSum - nums[i];
10+
if (leftSum == rightSum) {
11+
return i;
12+
}
13+
leftSum += nums[i];
14+
}
15+
return -1;
16+
}
17+
}

0 commit comments

Comments
 (0)