Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public static void main(String[] args) {
- Solution sol = new Solution();
- System.out.println(sol.solution("aaAbbcCABBc"));
- System.out.println(sol.solution("xyzXYZabcABC"));
- System.out.println(sol.solution("ABCabcAefG"));
- }
- public int solution(String letters) {
- int count = 0;
- Integer[] lastLowerCase = new Integer[26], firstUpperCase = new Integer[26];
- for (int i = 0; i < letters.length(); i++) {
- char charAtIndex = letters.charAt(i);
- // Is Lowercase?
- if (charAtIndex >= 'a' && charAtIndex <= 'z') {
- int charIndex = charAtIndex - 'a';
- lastLowerCase[charIndex] = i;
- }
- // Is Uppercase?
- else if (charAtIndex >= 'A' && charAtIndex <= 'Z') {
- int charIndex = charAtIndex - 'A';
- if (firstUpperCase[charIndex] == null)
- firstUpperCase[charIndex] = i;
- }
- }
- for (int i = 0; i < 26; i++) {
- if (lastLowerCase[i] != null && firstUpperCase[i] != null && lastLowerCase[i] < firstUpperCase[i]) {
- count++;
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment