Untitled

 avatar
unknown
plain_text
6 months ago
799 B
2
Indexable
public class Solution {
    public int func(String s) {
        int[] lastSeenIndex = new int[3];
        // Initialize all elements to -1
        for (int i = 0; i < 3; i++) {
            lastSeenIndex[i] = -1;
        }

        int cnt = 0;
        int n = s.length();

        for (int i = 0; i < n; i++) {
            // Update the last seen index for the current character
            lastSeenIndex[s.charAt(i) - 'a'] = i;

            // Check if all three characters have been seen
            if (lastSeenIndex[0] != -1 && lastSeenIndex[1] != -1 && lastSeenIndex[2] != -1) {
                // Add to the count
                cnt += 1 + Math.min(lastSeenIndex[0], Math.min(lastSeenIndex[1], lastSeenIndex[2]));
            }
        }

        return cnt;
    }
}
Editor is loading...
Leave a Comment