Skip to content

Commit afbd26d

Browse files
add 1271
1 parent 4ed7553 commit afbd26d

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _If you like this project, please leave me a star._ ★
1111
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | | | |Medium||
1212
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | | | |Easy||
1313
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java) | | | |Medium||
14+
|1271|[Hexspeak](https://leetcode.com/problems/hexspeak/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1271.java) | | | |Easy||
1415
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1267.java) | | | |Medium||
1516
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | | | |Easy||
1617
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | | | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* 1271. Hexspeak
9+
*
10+
* A decimal number can be converted to its Hexspeak representation by first converting it to an uppercase hexadecimal string,
11+
* then replacing all occurrences of the digit 0 with the letter O, and the digit 1 with the letter I.
12+
* Such a representation is valid if and only if it consists only of the letters in the set {"A", "B", "C", "D", "E", "F", "I", "O"}.
13+
* Given a string num representing a decimal integer N, return the Hexspeak representation of N if it is valid, otherwise return "ERROR".
14+
*
15+
* Example 1:
16+
* Input: num = "257"
17+
* Output: "IOI"
18+
* Explanation: 257 is 101 in hexadecimal.
19+
*
20+
* Example 2:
21+
* Input: num = "3"
22+
* Output: "ERROR"
23+
*
24+
* Constraints:
25+
* 1 <= N <= 10^12
26+
* There are no leading zeros in the given string.
27+
* All answers must be in uppercase letters.
28+
* */
29+
public class _1271 {
30+
public static class Solution1 {
31+
public String toHexspeak(String num) {
32+
long numInt = Long.parseLong(num);
33+
String hexString = Long.toHexString(numInt);
34+
StringBuilder sb = new StringBuilder();
35+
Set<Character> set = new HashSet<>(Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', '1', '0', 'a', 'b', 'c', 'd', 'e', 'f'));
36+
for (char c : hexString.toCharArray()) {
37+
if (!set.contains(c)) {
38+
return "ERROR";
39+
} else if (c == '1') {
40+
sb.append("I");
41+
} else if (c == '0') {
42+
sb.append("O");
43+
} else {
44+
sb.append(Character.toUpperCase(c));
45+
}
46+
}
47+
return sb.toString();
48+
}
49+
}
50+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1271;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1271Test {
10+
private static _1271.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1271.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("IOI", solution1.toHexspeak("257"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("ERROR", solution1.toHexspeak("3"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("ERROR", solution1.toHexspeak("619879596177"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("AEIDBCDIBC", solution1.toHexspeak("747823223228"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)