RenzCutie

SolutionTask3

Jul 23rd, 2025 (edited)
167
0
17 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. class Solution {
  2.   public static void main(String[] args) {
  3.     Solution sol = new Solution();
  4.  
  5.     System.out.println(sol.solution("aaAbbcCABBc"));
  6.     System.out.println(sol.solution("xyzXYZabcABC"));
  7.     System.out.println(sol.solution("ABCabcAefG"));
  8.   }
  9.  
  10.   public int solution(String letters) {
  11.     int count = 0;
  12.     Integer[] lastLowerCase = new Integer[26], firstUpperCase = new Integer[26];
  13.    
  14.     for (int i = 0; i < letters.length(); i++) {
  15.       char charAtIndex = letters.charAt(i);
  16.  
  17.       // Is Lowercase?
  18.       if (charAtIndex >= 'a' && charAtIndex <= 'z') {
  19.         int charIndex = charAtIndex - 'a';
  20.         lastLowerCase[charIndex] = i;
  21.       }
  22.       // Is Uppercase?
  23.       else if (charAtIndex >= 'A' && charAtIndex <= 'Z') {
  24.         int charIndex = charAtIndex - 'A';
  25.         if (firstUpperCase[charIndex] == null)
  26.           firstUpperCase[charIndex] = i;
  27.       }
  28.     }
  29.    
  30.     for (int i = 0; i < 26; i++) {
  31.       if (lastLowerCase[i] != null && firstUpperCase[i] != null && lastLowerCase[i] < firstUpperCase[i]) {
  32.         count++;
  33.       }
  34.     }
  35.    
  36.     return count;
  37.   }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment