Skip to content

Instantly share code, notes, and snippets.

@codetricity
Created November 9, 2021 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codetricity/790c39261a8f33ed2cabe90fdb4c99aa to your computer and use it in GitHub Desktop.
Save codetricity/790c39261a8f33ed2cabe90fdb4c99aa to your computer and use it in GitHub Desktop.
example of finding matches in a string
public class Main {
public static void main(String[] args) {
String data = "CCAAAAATTT!";
char maxLetter = data.charAt(0);
int maxCount = 1;
for (int i = 0; i < data.length() -1; i++) {
int currentCount = 1;
char letter = data.charAt(i);
while (letter == data.charAt(i + 1)) {
currentCount++;
i++;
// System.out.println("found match: " + letter + " = " + currentCount);
if (currentCount > maxCount) {
maxCount = currentCount;
maxLetter = data.charAt(i);
}
System.out.println(i);
}
// System.out.println("max count is " + maxCount);
// System.out.println("max letter is " + maxLetter);
}
System.out.println(maxLetter + " " + maxCount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment