Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4be100e

Browse files
authored
change StringCompression.java
I want to add a new method to check if the input string contains any number.
1 parent 9d705a5 commit 4be100e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/main/java/com/ctci/arraysandstrings/StringCompression.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ private static String compressString(String str) {
1717
StringBuilder compressedSb = new StringBuilder();
1818
int countConsecutive = 0;
1919
for (int i = 0; i < str.length(); i++) {
20+
if(stringContainsNumber(str)) {
21+
return str;
22+
}
2023
countConsecutive++;
2124

2225
/* If next character is different than current, append this char to result. */
@@ -28,6 +31,16 @@ private static String compressString(String str) {
2831
}
2932
return compressedSb.length() < str.length() ? compressedSb.toString() : str;
3033
}
34+
35+
private static boolean stringContainsNumber(String str) {
36+
int[] numArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
37+
for(int i = 0; i < numArr.length; i++) {
38+
if(str.contains(String.valueOf(numArr[i]))) {
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
3144

3245
public static void main(String[] args) {
3346
System.out.println("aabccccaaa: " + compressString("aabccccaaa"));

0 commit comments

Comments
 (0)