Skip to content

Commit 1965aab

Browse files
committed
Added 1 solution & refactored 2 solutions
1 parent b3bc443 commit 1965aab

3 files changed

+59
-41
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public String freqAlphabets(String s) {
3+
StringBuilder sb = new StringBuilder();
4+
int idx = 0;
5+
int n = s.length();
6+
while (idx < n) {
7+
char c = s.charAt(idx);
8+
int asciiAdd = -1;
9+
if (idx + 2 < n && s.charAt(idx + 2) == '#') {
10+
asciiAdd = Integer.parseInt(s.substring(idx, idx + 2));
11+
idx += 3;
12+
}
13+
else {
14+
asciiAdd = Integer.parseInt(s.substring(idx, idx + 1));
15+
idx++;
16+
}
17+
sb.append((char) (96 + asciiAdd));
18+
}
19+
return sb.toString();
20+
}
21+
}

Easy/Repeated Substring Pattern.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
class Solution {
2-
public boolean repeatedSubstringPattern(String s) {
3-
String s1 = s + s;
4-
String s2 = s1.substring(1,s1.length()-1);
5-
6-
return s2.contains(s);
2+
public boolean repeatedSubstringPattern(String s) {
3+
int n = s.length();
4+
for (int i = n / 2; i >= 0; i--) {
5+
if (i != 0 && n % i == 0) {
6+
int count = n / i;
7+
StringBuilder sb = new StringBuilder();
8+
String sub = s.substring(0, i);
9+
while (count-- > 0) {
10+
sb.append(sub);
11+
}
12+
if (sb.toString().equals(s)) {
13+
return true;
14+
}
15+
}
716
}
17+
return false;
18+
}
819
}

Medium/Find Peak Element.java

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
11
class Solution {
2-
public static int findPeakElement(int[] nums) {
3-
if (nums.length == 1) {
4-
return 0;
5-
}
6-
7-
return findPeakElementImpl(nums, 0, nums.length-1);
8-
}
9-
10-
private static int findPeakElementImpl(int[] nums, int start, int end) {
11-
if (start > end) {
12-
return -1;
13-
}
14-
15-
int mid = (start + end)/2;
16-
17-
if (mid == 0 && nums.length > 1) {
18-
if (nums[mid] > nums[mid+1]) {
19-
return mid;
20-
}
21-
}
22-
else if (mid == nums.length-1 && nums.length > 1) {
23-
if (nums[mid] > nums[mid-1]) {
24-
return mid;
25-
}
26-
}
27-
else {
28-
if (nums.length > 1) {
29-
if (nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1]) {
30-
return mid;
31-
}
32-
}
33-
}
34-
35-
int left = findPeakElementImpl(nums, start, mid-1);
36-
37-
return left == -1 ? findPeakElementImpl(nums, mid+1, end) : left;
2+
public int findPeakElement(int[] nums) {
3+
int[] index = {-1};
4+
helper(nums, 0, nums.length - 1, index);
5+
return index[0];
6+
}
7+
8+
private void helper(int[] nums, int start, int end, int[] index) {
9+
if (start <= end && index[0] == -1) {
10+
int mid = (start + end) / 2;
11+
boolean found = (
12+
(mid + 1 < nums.length ? nums[mid] > nums[mid + 1] : true) &&
13+
(mid - 1 >= 0 ? nums[mid] > nums[mid - 1] : true)
14+
);
15+
if (found) {
16+
index[0] = mid;
17+
return;
18+
}
19+
else {
20+
helper(nums, start, mid - 1, index);
21+
helper(nums, mid + 1, end, index);
22+
}
3823
}
24+
}
3925
}

0 commit comments

Comments
 (0)