-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathYttrium2019.java
69 lines (62 loc) · 2.21 KB
/
Yttrium2019.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.lang.Math;
import java.util.stream.Stream;
class Solution {
class Range
{
int Left = Integer.MAX_VALUE;
int Right = Integer.MIN_VALUE;
}
private static int getCharIndex(char c)
{
return Character.getNumericValue(c) - Character.getNumericValue('a');
}
public int solution(String S, int K)
{
final int allLowerCaseLetters = 26;
int[] letterCounts = new int[allLowerCaseLetters];
int uniqueLetters = 0;
Range[] ranges = new Range[allLowerCaseLetters];
for ( int i = 0 ; i < allLowerCaseLetters ; ++i) ranges[i] = new Range();
for ( int i = 0 ; i < S.length() ; ++i)
{
int charIndex = getCharIndex(S.charAt(i));
ranges[charIndex].Left = Math.min(ranges[charIndex].Left,i);
ranges[charIndex].Right = Math.max(ranges[charIndex].Right,i);
if ( letterCounts[charIndex] == 0) ++uniqueLetters;
letterCounts[charIndex]++;
}
final int toRemoveK = uniqueLetters - K;
if (toRemoveK < 0 ) return -1;
if (toRemoveK == 0 ) return 0;
int minLen = Integer.MAX_VALUE;;
for ( Range a : ranges)
{
for ( Range b : ranges)
{
int removed = 0;
int Left = a.Left;
int Right = b.Right;
for ( Range c : ranges)
{
if (c.Left != Integer.MAX_VALUE && c.Left >= Left && c.Right <= Right) ++removed;
}
if ( removed == toRemoveK)
{
minLen = Math.min(minLen, Right - Left + 1);
}
}
}
return minLen;
}
public static void main(String[] args)
{
Solution sol = new Solution();
System.out.println(sol.solution("zaaaa",1));
System.out.println(sol.solution("abaacbca",2));
System.out.println(sol.solution("aabcabc",1));
}
}