Skip to content

Commit 796a99d

Browse files
committed
Added 1 solution & modified 1 solution
1 parent 38c6fa2 commit 796a99d

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

Medium/Implement Rand10() Using Rand7().java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
* @return a random integer in the range 1 to 7
55
*/
66
class Solution extends SolBase {
7-
public int rand10() {
8-
int temp = 0;
9-
while (true) {
10-
temp = (rand7() - 1) * 7 + (rand7() - 1);
11-
if (temp < 40) {
12-
break;
13-
}
14-
}
15-
16-
return temp % 10 + 1;
7+
public int rand10() {
8+
int temp = 0;
9+
while (true) {
10+
temp = (rand7() - 1) * 7 + (rand7() - 1);
11+
if (temp < 40) {
12+
break;
13+
}
1714
}
15+
return temp % 10 + 1;
16+
}
1817
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int maxRepOpt1(String text) {
3+
int[] count = new int[26];
4+
for (char c : text.toCharArray()) {
5+
count[c - 'a']++;
6+
}
7+
int maxCount = 0;
8+
int i = 0;
9+
while (i < text.length()) {
10+
char c = text.charAt(i);
11+
int curr = i;
12+
int currCount = 0;
13+
int diff = 0;
14+
// To skip the same characters before we make a swap
15+
int swapPoint = i;
16+
while (curr < text.length() && (text.charAt(curr) == c || diff == 0) && currCount < count[c - 'a']) {
17+
if (text.charAt(curr) != c) {
18+
diff++;
19+
swapPoint = curr - 1;
20+
}
21+
currCount++;
22+
curr++;
23+
}
24+
maxCount = Math.max(maxCount, currCount);
25+
i = swapPoint + 1;
26+
}
27+
return maxCount;
28+
}
29+
}

0 commit comments

Comments
 (0)