Skip to content

Commit 0c646a3

Browse files
committed
Added 4 solutions
1 parent 7ae411a commit 0c646a3

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
class Solution {
2+
public static String maskPII(String S) {
3+
if (S.length() == 0) {
4+
return S;
5+
}
6+
7+
if (S.indexOf('@') != -1) {
8+
return formatEmail(S);
9+
}
10+
else {
11+
return formatNumber(S);
12+
}
13+
}
14+
15+
private static String formatNumber(String s) {
16+
StringBuilder sb = new StringBuilder();
17+
int digitCount = getDigitCount(s);
18+
19+
if (digitCount > 10) {
20+
sb.append("+");
21+
int extra = digitCount - 10;
22+
while (extra-- > 0) {
23+
sb.append("*");
24+
}
25+
sb.append("-");
26+
}
27+
sb.append("***-***-");
28+
29+
StringBuilder last4 = new StringBuilder();
30+
for (int i=s.length()-1; i>=0; i--) {
31+
if (Character.isDigit(s.charAt(i))) {
32+
last4.append(s.charAt(i));
33+
}
34+
35+
if (last4.length() == 4) {
36+
break;
37+
}
38+
}
39+
40+
sb.append(last4.reverse().toString());
41+
42+
return sb.toString();
43+
}
44+
45+
private static int getDigitCount(String s) {
46+
char[] chars = s.toCharArray();
47+
int count = 0;
48+
49+
for (char c : chars) {
50+
if (Character.isDigit(c)) {
51+
count++;
52+
}
53+
}
54+
55+
return count;
56+
}
57+
58+
private static String formatEmail(String s) {
59+
int idx = s.indexOf('@');
60+
int revStart = idx - 1;
61+
while (revStart >= 0 && Character.isLetter(s.charAt(revStart))) {
62+
revStart--;
63+
}
64+
65+
StringBuilder sb = new StringBuilder();
66+
67+
sb.
68+
append(s.substring(0, revStart+1)).
69+
append(Character.toLowerCase(s.charAt(revStart + 1))).
70+
append("*****").
71+
append(Character.toLowerCase(s.charAt(s.indexOf('@') - 1))).
72+
append(s.substring(s.indexOf('@')));
73+
74+
return sb.toString().toLowerCase();
75+
}
76+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int widthOfBinaryTree(TreeNode root) {
12+
if (root == null) {
13+
return 0;
14+
}
15+
16+
List<TreeNode> list = new ArrayList<>();
17+
list.add(root);
18+
int res = 1;
19+
20+
while (list.size() != 0) {
21+
List<TreeNode> temp = new ArrayList<>();
22+
for (TreeNode node : list) {
23+
if(node == null) {
24+
temp.add(null);
25+
temp.add(null);
26+
}
27+
else {
28+
temp.add(node.left);
29+
temp.add(node.right);
30+
}
31+
}
32+
33+
temp = removeExtraNull(temp);
34+
res = Math.max(res, temp.size());
35+
list = temp;
36+
}
37+
38+
return res;
39+
}
40+
41+
private List<TreeNode> removeExtraNull(List<TreeNode> list) {
42+
List<TreeNode> ans = new ArrayList<>();
43+
if (list.size() == 0) {
44+
return ans;
45+
}
46+
47+
int start = 0;
48+
int end = list.size() - 1;
49+
50+
while (start <= end && list.get(start) == null) {
51+
start++;
52+
}
53+
54+
while (end >= 0 && list.get(end) == null) {
55+
end--;
56+
}
57+
58+
while (start <= end) {
59+
ans.add(list.get(start++));
60+
}
61+
62+
return ans;
63+
}
64+
}

Medium/Super Ugly Number.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public static int nthSuperUglyNumber(int n, int[] primes) {
3+
int[] arr = new int[n+1];
4+
arr[0] = 1;
5+
6+
int[] counter = new int[primes.length];
7+
8+
for (int i=1; i<n; i++) {
9+
int temp = Integer.MAX_VALUE;
10+
int idx = -1;
11+
12+
for (int j=0; j<primes.length; j++) {
13+
if (primes[j]*arr[counter[j]] < temp) {
14+
temp = primes[j]*arr[counter[j]];
15+
idx = j;
16+
}
17+
else if (primes[j]*arr[counter[j]] == temp) {
18+
counter[j]++;
19+
}
20+
}
21+
22+
arr[i] = temp;
23+
counter[idx]++;
24+
}
25+
26+
return arr[n-1];
27+
}
28+
}

Medium/Ugly Number II.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public static int nthUglyNumber(int n) {
3+
int[] arr = new int[n];
4+
arr[0] = 1;
5+
int i2 = 0;
6+
int i3 = 0;
7+
int i5 = 0;
8+
9+
int mul2 = 2;
10+
int mul3 = 3;
11+
int mul5 = 5;
12+
13+
int ugly = 1;
14+
15+
for (int i=1; i<n; i++) {
16+
ugly = Math.min(mul2, Math.min(mul3, mul5));
17+
arr[i] = ugly;
18+
19+
if (ugly == mul2) {
20+
i2++;
21+
mul2 = arr[i2]*2;
22+
}
23+
if (ugly == mul3) {
24+
i3++;
25+
mul3 = arr[i3]*3;
26+
}
27+
if (ugly == mul5) {
28+
i5++;
29+
mul5 = arr[i5]*5;
30+
}
31+
}
32+
33+
return ugly;
34+
}
35+
}

0 commit comments

Comments
 (0)