Skip to content

Commit 524d18f

Browse files
committed
Added 1 solution & refactored 2 solutions
1 parent 5bd07f5 commit 524d18f

File tree

4 files changed

+47
-57
lines changed

4 files changed

+47
-57
lines changed

Easy/Array Partition I.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int arrayPairSum(int[] nums) {
3+
Arrays.sort(nums);
4+
int sum = 0;
5+
for (int i = 0; i < nums.length; i += 2) {
6+
sum += nums[i];
7+
}
8+
9+
return sum;
10+
}
11+
}

Easy/Array Partition.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

Easy/Fixed Point.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int fixedPoint(int[] A) {
3+
int low = 0;
4+
int high = A.length - 1;
5+
6+
while (low < high) {
7+
int mid = (low + high) / 2;
8+
if (A[mid] - mid < 0) {
9+
low = mid + 1;
10+
}
11+
else {
12+
high = mid;
13+
}
14+
}
15+
16+
return low == A[low] ? low : -1;
17+
}
18+
}

Medium/Custom Sort String.java

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,27 @@
11
class Solution {
2-
public String customSortString(String s, String t) {
3-
4-
// Separate out the characters from t which are not part of s
5-
StringBuilder nonS = new StringBuilder();
6-
for (int i=0; i<t.length(); i++) {
7-
if (s.indexOf(t.charAt(i)) == -1) {
8-
nonS.append(t.charAt(i));
9-
}
2+
public String customSortString(String S, String T) {
3+
int[] counter = new int[26];
4+
for (char c : T.toCharArray()) {
5+
counter[c - 'a']++;
106
}
11-
12-
// Create a character array of t with words present in s
13-
Character[] arr = new Character[t.length() - nonS.length()];
14-
for (int i=0,j=0; i<t.length(); i++) {
15-
if (s.indexOf(t.charAt(i)) != -1) {
16-
arr[j++] = t.charAt(i);
7+
8+
StringBuilder sb = new StringBuilder();
9+
for (char c : S.toCharArray()) {
10+
int count = counter[c - 'a'];
11+
for (int i = 0; i < count; i++) {
12+
sb.append(c);
1713
}
14+
15+
counter[c - 'a'] = 0;
1816
}
19-
20-
// Create a value map for characters in s with increasing counter
21-
int counter = 0;
22-
Map<Character, Integer> map = new HashMap<>();
23-
for (char c : s.toCharArray()) {
24-
map.put(c, counter);
25-
counter++;
26-
}
27-
28-
// Sort the character array based on value map
29-
Arrays.sort(arr, new Comparator<Character>() {
30-
@Override
31-
public int compare(Character o1, Character o2) {
32-
return map.get(o1) - map.get(o2);
17+
18+
for (char c ='a'; c <= 'z'; c++) {
19+
int count = counter[c - 'a'];
20+
for (int i = 0; i < count; i++) {
21+
sb.append(c);
3322
}
34-
});
35-
36-
// Prepare the final string answer
37-
StringBuilder sb = new StringBuilder();
38-
for (Character c : arr) {
39-
sb.append(c);
4023
}
41-
42-
// Append characters not present in s to the end of solution
43-
sb.append(nonS.toString());
44-
24+
4525
return sb.toString();
4626
}
4727
}

0 commit comments

Comments
 (0)