forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-2994 - String consists of substrings example (eugenp#7190)
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
java-strings-2/src/main/java/com/baeldung/string/repetition/SubstringRepetition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.baeldung.string.repetition; | ||
|
||
public class SubstringRepetition { | ||
|
||
public static boolean containsOnlySubstrings(String string) { | ||
|
||
if (string.length() < 2) { | ||
return false; | ||
} | ||
|
||
StringBuilder substr = new StringBuilder(); | ||
for (int i = 0; i < string.length() / 2; i++) { | ||
substr.append(string.charAt(i)); | ||
|
||
String clearedFromSubstrings = string.replaceAll(substr.toString(), ""); | ||
|
||
if (clearedFromSubstrings.length() == 0) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static boolean containsOnlySubstringsEfficient(String string) { | ||
|
||
return ((string + string).indexOf(string, 1) != string.length()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
java-strings-2/src/test/java/com/baeldung/string/repetition/SubstringRepetitionUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.baeldung.string.repetition; | ||
|
||
import static com.baeldung.string.repetition.SubstringRepetition.*; | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
public class SubstringRepetitionUnitTest { | ||
|
||
private String validString = "aa"; | ||
private String validStringTwo = "ababab"; | ||
private String validStringThree = "aabcaabcaabcaabc"; | ||
|
||
private String invalidString = "aca"; | ||
private String invalidStringTwo = "ababa"; | ||
private String invalidStringThree = "abcdab"; | ||
|
||
@Test | ||
public void givenValidStrings_whenCheckIfContainsOnlySubstrings_thenReturnsTrue() { | ||
assertTrue(containsOnlySubstrings(validString)); | ||
assertTrue(containsOnlySubstrings(validStringTwo)); | ||
assertTrue(containsOnlySubstrings(validStringThree)); | ||
} | ||
|
||
@Test | ||
public void givenInvalidStrings_whenCheckIfContainsOnlySubstrings_thenReturnsFalse() { | ||
assertFalse(containsOnlySubstrings(invalidString)); | ||
assertFalse(containsOnlySubstrings(invalidStringTwo)); | ||
assertFalse(containsOnlySubstrings(invalidStringThree)); | ||
} | ||
|
||
@Test | ||
public void givenValidStrings_whenCheckEfficientlyIfContainsOnlySubstrings_thenReturnsTrue() { | ||
assertTrue(containsOnlySubstringsEfficient(validString)); | ||
assertTrue(containsOnlySubstringsEfficient(validStringTwo)); | ||
assertTrue(containsOnlySubstringsEfficient(validStringThree)); | ||
} | ||
|
||
@Test | ||
public void givenInvalidStrings_whenCheckEfficientlyIfContainsOnlySubstrings_thenReturnsFalse() { | ||
assertFalse(containsOnlySubstringsEfficient(invalidString)); | ||
assertFalse(containsOnlySubstringsEfficient(invalidStringTwo)); | ||
assertFalse(containsOnlySubstringsEfficient(invalidStringThree)); | ||
} | ||
} |