Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public class LongestContiguousCharacterNaive {
  2.  
  3. public void find(String input){
  4.  
  5. if(input==null || input.trim().length()==0)
  6. return;
  7.  
  8. int maxCount = 1;
  9. int currCount = 1;
  10. char maxChar = input.charAt(0);
  11. char currChar = input.charAt(0);;
  12. for (int i = 0; i <input.length(); i++) {
  13. currChar = input.charAt(i);
  14. currCount = 1;
  15. for (int j = i+1; j <input.length() ; j++) {
  16. if(currChar==input.charAt(j)){
  17. currCount++;
  18. if(currCount>maxCount){
  19. maxCount = currCount;
  20. maxChar = currChar;
  21. }
  22. }else{
  23. break;
  24. }
  25. }
  26. }
  27.  
  28. System.out.println("Input: " + input + ", Longest Contiguous Character: " + maxChar + " with count: " + maxCount);
  29. }
  30.  
  31. public static void main(String[] args) {
  32.  
  33. LongestContiguousCharacterNaive l = new LongestContiguousCharacterNaive();
  34. String str = "aaaabbaacccccde";
  35. l.find(str);
  36. str = "bbbbbbb";
  37. l.find(str);
  38. str = "abcdeaab";
  39. l.find(str);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement