Skip to content

Instantly share code, notes, and snippets.

@EasyG0ing1
Last active March 16, 2023 09:49
Show Gist options
  • Save EasyG0ing1/f6e1d2c18d41850f80fa42bad469c9eb to your computer and use it in GitHub Desktop.
Save EasyG0ing1/f6e1d2c18d41850f80fa42bad469c9eb to your computer and use it in GitHub Desktop.
Method to find any repeating patterns inside a String. Output will show each repeating pattern and how many times it repeats.
public class StringPatterns {
//This is used to convert all message contents into original text. It converts the unicode automatically.
private String decodeString(String text) {
Charset targetEncoding = Charset.forName("ISO-8859-1");
String unescapeText = StringEscapeUtils.unescapeJava(text);
return new String(unescapeText.getBytes(targetEncoding), StandardCharsets.UTF_8);
}
}
public void usungApache() {
int length = 10;
boolean useLetters = true;
boolean useNumbers = false;
String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
System.out.println(generatedString);
}
static String getAlphaNumericString(int n) {
byte[] array = new byte[256];
new Random().nextBytes(array);
String randomString = new String(array, StandardCharsets.UTF_8);
StringBuffer r = new StringBuffer();
for (int k = 0; k < randomString.length(); k++) {
char ch = randomString.charAt(k);
if (((ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9'))
&& (n > 0)) {
r.append(ch);
n--;
}
}
return r.toString();
}
public void givenUsingJava8_whenGeneratingRandomAlphabeticString_thenCorrect() {
int leftLimit = 97; // letter 'a'
int rightLimit = 122; // letter 'z'
int targetStringLength = 10;
Random random = new Random();
String generatedString = random.ints(leftLimit, rightLimit + 1)
.limit(targetStringLength)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
System.out.println(generatedString);
}
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
class StringPatterns {
private void checkForPattern(String userString) {
String buildString;
LinkedList<String> patterns = new LinkedList<>();
int size = userString.length();
int hits;
int newSize;
String[] coreString = new String[size];
Map<String, Integer> hitCountMap = new HashMap<>();
for (int x = 0; x < size; x++) {
coreString[x] = userString.substring(x, x + 1);
}
for (int index = 0; index < size - 1; index++) {
buildString = coreString[index];
for (int x = index + 1; x < size; x++) {
buildString = buildString + coreString[x];
patterns.add(buildString);
}
}
for (String pattern : patterns) {
String check = userString.replaceFirst(pattern, "");
if (check.contains(pattern)) {
newSize = userString.replaceAll(pattern, "").length();
hits = (size - newSize) / pattern.length();
hitCountMap.put(pattern, hits);
}
}
for (String pattern : hitCountMap.keySet()) {
System.out.println("Pattern: " + pattern +
" repeated " + hitCountMap.get(pattern) +
" times.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment