Skip to content

Commit c2affb5

Browse files
committed
leetcode: DecryptIntegerAlphabet: passed
leetcode: FindTheDifference: passed leetcode: GoalParserInterpretation: passed leetcode: MatrixDiagonalSum: passed leetcode: MergeStringsAlternately: passed leetcode: VerifyingAlienDictionary: passed
1 parent 31c833f commit c2affb5

12 files changed

+295
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.leetcode.array;
2+
3+
public final class MatrixDiagonalSum {
4+
private MatrixDiagonalSum() {
5+
6+
}
7+
8+
public static int diagonalSum(int[][] mat) {
9+
int sum = 0;
10+
for (int i = 0; i < mat.length; i++) {
11+
for (int j = 0; j < mat[i].length; j++) {
12+
if (i == j || i + j == mat.length - 1) {
13+
sum += mat[i][j];
14+
}
15+
}
16+
}
17+
return sum;
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.leetcode.array;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
8+
import java.util.stream.Stream;
9+
10+
class MatrixDiagonalSumTest {
11+
12+
static Stream<Arguments> testCases() {
13+
return Stream.of(
14+
Arguments.of(new int[][] {{1, 2, 3},
15+
{4, 5, 6},
16+
{7, 8, 9}}, 25)
17+
);
18+
}
19+
20+
@ParameterizedTest
21+
@MethodSource("testCases")
22+
void testDiagonalSum(int[][] mat, int expected) {
23+
int actual = MatrixDiagonalSum.diagonalSum(mat);
24+
Assertions.assertEquals(expected, actual);
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.leetcode.string;
2+
3+
public final class DecryptIntegerAlphabet {
4+
5+
private static final int BASE = 10;
6+
7+
private DecryptIntegerAlphabet() {
8+
}
9+
10+
public static String freqAlphabets(String s) {
11+
StringBuilder sb = new StringBuilder();
12+
int i = 0;
13+
while (i < s.length()) {
14+
final int code;
15+
if (i + 2 < s.length() && s.charAt(i + 2) == '#') {
16+
code = readDigit(s, i) * BASE + readDigit(s, i + 1);
17+
i += 3;
18+
} else {
19+
code = readDigit(s, i);
20+
i++;
21+
}
22+
23+
sb.append((char) ('a' + code - 1));
24+
}
25+
return sb.toString();
26+
}
27+
28+
private static int readDigit(String s, int i) {
29+
return s.charAt(i) - '0';
30+
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.leetcode.string;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
class DecryptIntegerAlphabetTest {
8+
9+
@ParameterizedTest
10+
@CsvSource( {
11+
"10#11#12,jkab"
12+
})
13+
void testFreqAlphabets(String s, String expected) {
14+
String actual = DecryptIntegerAlphabet.freqAlphabets(s);
15+
Assertions.assertEquals(expected, actual);
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.leetcode.string;
2+
3+
public final class FindTheDifference {
4+
5+
private static final int ALPHABET_SIZE = 26;
6+
7+
private FindTheDifference() {
8+
}
9+
10+
public static char findTheDifference(String s, String t) {
11+
char[] dict1 = dict(s);
12+
char[] dict2 = dict(t);
13+
for (int i = 0; i < ALPHABET_SIZE; i++) {
14+
if (dict2[i] != dict1[i]) {
15+
return (char) ('a' + i);
16+
}
17+
}
18+
throw new IllegalStateException();
19+
}
20+
21+
private static char[] dict(String s) {
22+
char[] dict = new char[ALPHABET_SIZE];
23+
for (int i = 0; i < s.length(); i++) {
24+
int index = s.charAt(i) - 'a';
25+
dict[index]++;
26+
}
27+
return dict;
28+
}
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.leetcode.string;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
class FindTheDifferenceTest {
8+
9+
@ParameterizedTest
10+
@CsvSource( {
11+
"abcd,abcde,e",
12+
"'',y,y"
13+
})
14+
void testFindTheDifference(String s, String t, char expected) {
15+
char actual = FindTheDifference.findTheDifference(s, t);
16+
Assertions.assertEquals(expected, actual);
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.leetcode.string;
2+
3+
public final class GoalParserInterpretation {
4+
private GoalParserInterpretation() {
5+
}
6+
7+
public static String interpret(String command) {
8+
int i = 0;
9+
StringBuilder sb = new StringBuilder();
10+
while (i < command.length()) {
11+
if (command.charAt(i) == 'G') {
12+
sb.append('G');
13+
i++;
14+
} else {
15+
if (command.charAt(i) == '(' && command.charAt(i + 1) == ')') {
16+
sb.append('o');
17+
i += 2;
18+
} else {
19+
sb.append("al");
20+
i += 4;
21+
}
22+
}
23+
}
24+
return sb.toString();
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.leetcode.string;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
class GoalParserInterpretationTest {
8+
9+
@ParameterizedTest
10+
@CsvSource( {
11+
"G()(al),Goal",
12+
"G()()()()(al),Gooooal",
13+
"(al)G(al)()()G,alGalooG"
14+
})
15+
void testInterpret(String s, String expected) {
16+
String actual = GoalParserInterpretation.interpret(s);
17+
Assertions.assertEquals(expected, actual);
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.leetcode.string;
2+
3+
public final class MergeStringsAlternately {
4+
private MergeStringsAlternately() {
5+
}
6+
7+
public static String mergeAlternately(String word1, String word2) {
8+
char[] res = new char[word1.length() + word2.length()];
9+
int j = 0;
10+
int i = 0;
11+
int k = 0;
12+
while (k < res.length) {
13+
if (i < word1.length()) {
14+
res[k] = word1.charAt(i);
15+
i++;
16+
k++;
17+
}
18+
if (j < word2.length()) {
19+
res[k] = word2.charAt(j);
20+
j++;
21+
k++;
22+
}
23+
}
24+
return new String(res);
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.leetcode.string;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
6+
7+
class MergeStringsAlternatelyTest {
8+
9+
@ParameterizedTest
10+
@CsvSource( {
11+
"abc,pqr,apbqcr",
12+
"ab,pqrs,apbqrs"
13+
})
14+
void testMergeAlternately(String s1, String s2, String expected) {
15+
String actual = MergeStringsAlternately.mergeAlternately(s1, s2);
16+
Assertions.assertEquals(expected, actual);
17+
}
18+
}

0 commit comments

Comments
 (0)