Skip to content

Commit 4626e39

Browse files
add a method to convert char array
1 parent b45ab1d commit 4626e39

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/main/java/com/fishercoder/common/utils/CommonUtils.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,40 @@ public static void print2DCharArray(char[][] arrayArrays) {
264264
System.out.println();
265265
}
266266

267+
public static char[][] convertLeetCodeRegular2DCharArrayInputIntoJavaArray(String input) {
268+
/**LeetCode 2-d char array usually comes in like this:
269+
* [["#"," ","#"],[" "," ","#"],["#","c"," "]] which is wrapped in double quotes instead of single quotes which makes it not usable in Java code.
270+
* This method helps with the conversion.*/
271+
input = input.substring(1, input.length() - 1);
272+
String[] arrays = input.split("],\\[");
273+
// CommonUtils.printArray_generic_type(arrays);
274+
int m = arrays.length;
275+
int n = arrays[1].split(",").length;
276+
char[][] ans = new char[m][n];
277+
for (int i = 0; i < m; i++) {
278+
if (i == 0) {
279+
String str = arrays[i].substring(1);
280+
String[] strs = str.split(",");
281+
for (int j = 0; j < strs.length; j++) {
282+
ans[i][j] = strs[j].charAt(1);
283+
}
284+
} else if (i == m - 1) {
285+
String str = arrays[i].substring(0, arrays[i].length() - 1);
286+
String[] strs = str.split(",");
287+
for (int j = 0; j < strs.length; j++) {
288+
ans[i][j] = strs[j].charAt(1);
289+
}
290+
} else {
291+
String[] strs = arrays[i].split(",");
292+
for (int j = 0; j < strs.length; j++) {
293+
ans[i][j] = strs[j].charAt(1);
294+
}
295+
}
296+
}
297+
CommonUtils.print2DCharArray(ans);
298+
return ans;
299+
}
300+
267301
public static int[][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray(String input) {
268302
/**
269303
* LeetCode 2-d array input usually comes like this: it's a REGULAR rectangle

src/test/java/com/fishercoder/_2018Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fishercoder;
22

3+
import com.fishercoder.common.utils.CommonUtils;
34
import com.fishercoder.solutions._2018;
45
import org.junit.BeforeClass;
56
import org.junit.Test;
@@ -77,4 +78,11 @@ public void test7() {
7778
}, "ca"));
7879
}
7980

81+
@Test
82+
public void test8() {
83+
assertEquals(true, solution1.placeWordInCrossword(
84+
CommonUtils.convertLeetCodeRegular2DCharArrayInputIntoJavaArray("[[\"#\",\" \",\"#\"],[\" \",\" \",\"#\"],[\"#\",\"c\",\" \"]]"),
85+
"abc"));
86+
}
87+
8088
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy