Skip to content

Commit

Permalink
test(U4_FRQ.java): test if solution works
Browse files Browse the repository at this point in the history
  • Loading branch information
101zh committed Nov 6, 2023
1 parent c4f65e2 commit 581e60f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/java/Unit4/U4_FRQ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Unit4;

public class U4_FRQ {
public static void main(String[] args) {
longestStreak("CCCCCCAAAAATTT!!!!!!!");
}

public static void longestStreak(String str) {

int longestStreak = 1;
int curStreak = 1;
String longestLetter = str.substring(0, 1);

for (int i = 1; i < str.length(); i++) {
String curLetter = str.substring(i, i + 1);
String prevLetter = str.substring(i - 1, i);

if (curLetter.equals(prevLetter)) {
curStreak++;
} else {
curStreak = 1;
}

if (curStreak > longestStreak) {
longestStreak = curStreak;
longestLetter = prevLetter;
}

}

System.out.println(longestLetter + " " + longestStreak);
}
}

0 comments on commit 581e60f

Please sign in to comment.