Advertisement
rajeshinternshala

Untitled

Oct 20th, 2023
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. public class Main {
  2.     public static int countReversals(String str, int k) {
  3.         int count = 0;
  4.         int n = str.length();
  5.  
  6.         for (int i = 0; i <= n - k; i++) {
  7.             String substring = str.substring(i, i + k);
  8.             String reversedSubstring = new StringBuilder(substring).reverse().toString();
  9.  
  10.             if (reversedSubstring.compareTo(substring) < 0) {
  11.                 count++;
  12.             }
  13.         }
  14.  
  15.         return count;
  16.     }
  17.  
  18.     public static void main(String[] args) {
  19.         String input = "aaaaa";
  20.         int k = 4;
  21.  
  22.         int result = countReversals(input, k);
  23.         System.out.println(result);
  24.     }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement