File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments