-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem3.java
More file actions
151 lines (121 loc) · 4.81 KB
/
Problem3.java
File metadata and controls
151 lines (121 loc) · 4.81 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*********************************************************************************************************
*
* Author: Maksim Markov, e-mail: maksim.markov.bg@gmail.com , mobile 088 6 839 991
* Date: 18.02.2015
*
**********************************************************************************************************/
package com.hackbulgaria.tasks;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
public class Problem3 {
private int currentStartIndex = 0;
private int finalStartIndex = 0;
private int finalEndIndex = 0;
private int currentLen = 0;
private int minLen = Integer.MAX_VALUE;
private char currentChar = ' ';
private boolean isStarted = false;
private boolean isFound = false;
Map<Character, Integer> alphabetMap = new HashMap<>();
Map<Character, Integer> encountered = new HashMap<>();
HashSet<Character> charsCovered = new HashSet<>();
private void updateEncounteredMapAndCharSet(Character currentChar, Map<Character,Integer> encounteredChar) {
Character ch = Character.toLowerCase(currentChar);
if(encounteredChar.containsKey(ch)) {
encounteredChar.put(ch, encounteredChar.get(ch)+1);
} else {
encounteredChar.put(ch, 1);
}
charsCovered.add(ch);
}
public String smallestSubstringContainingTheAlphabet(String strInput){
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String str = toLowerChars(strInput);
resetVariables();
instantiateAlphabetMap(alphabet, alphabetMap);
int charsToBeCovered = alphabetMap.size();
for(int i=0; i<str.length(); i++) {
if(21>str.charAt(i) || 255<str.charAt(i)){
return "Error: The string has no ASCII character.";
} else if (Character.isWhitespace(str.charAt(i)))
return "Error: The string contains white space.";
}
for(int i=0; i<str.length(); i++){
currentChar=str.charAt(i);
if(alphabetMap.containsKey(currentChar) && !isFound) {
if(!isStarted && !isFound) {
isStarted = true;
currentStartIndex = i;
}
updateEncounteredMapAndCharSet(currentChar, encountered);
if(charsCovered.size()==charsToBeCovered){
currentLen = i - currentStartIndex;
isFound = true;
updateMinLength(i);
for(int j=0; j<(i-alphabetMap.size()+1); j++) {
if(charsCovered.contains(str.charAt(currentStartIndex)) && encountered.get(str.charAt(currentStartIndex))>1) {
encountered.put(str.charAt(currentStartIndex), (encountered.get(str.charAt(currentStartIndex))-1));
currentStartIndex++;
} else if(!charsCovered.contains(str.charAt(currentStartIndex))){
currentStartIndex++;
}
}
currentLen = i - currentStartIndex;
updateMinLength(i);
}
} else if (alphabetMap.containsKey(currentChar) && isFound) {
updateEncounteredMapAndCharSet(currentChar, encountered);
for(int j=currentStartIndex; j<(i-alphabetMap.size()+1); j++) {
if(charsCovered.contains(str.charAt(currentStartIndex)) && encountered.get(str.charAt(currentStartIndex))>1) {
encountered.put(str.charAt(currentStartIndex), (encountered.get(str.charAt(currentStartIndex))-1));
currentStartIndex++;
} else if(!charsCovered.contains(str.charAt(currentStartIndex))){
currentStartIndex++;
}
}
currentLen = i - currentStartIndex;
updateMinLength(i);
}
} // end str.length()
return strInput.substring(finalStartIndex, finalEndIndex+1);
}
private void instantiateAlphabetMap(String str, Map<Character,Integer> enMap) {
for (char c : str.toCharArray())
enMap.put(Character.toLowerCase(c), 1);
}
private void updateMinLength(int index) {
if (minLen > currentLen) {
minLen = currentLen;
finalStartIndex = currentStartIndex;
finalEndIndex = index;
}
}
private String toLowerChars(String string){
char[] chars = new char[string.length()];
for(int i=0; i<string.length(); i++){
chars[i] = Character.toLowerCase(string.charAt(i));
}
return new String(chars);
}
private void resetVariables(){
currentStartIndex = 0;
finalStartIndex = 0;
finalEndIndex = 0;
currentLen = 0;
minLen = Integer.MAX_VALUE;
currentChar = ' ';
isStarted = false;
isFound = false;
encountered.clear();
charsCovered.clear();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Problem3 prob3 = new Problem3();
System.out.println(prob3.smallestSubstringContainingTheAlphabet("###aaaaaabcdefghijklmnopqrstuvwxyz"));
System.out.println(prob3.smallestSubstringContainingTheAlphabet("1abaaaaa333bcdefghijklmnopqrstuvwxyz"));
System.out.println(prob3.smallestSubstringContainingTheAlphabet("a%%%%b4444cdefghijklmn124345678!@#$%^&*opqrstuvwxyza!*abcdefgh$$$$$$$$$$$$$$$$$ijklmn"));
System.out.println(prob3.smallestSubstringContainingTheAlphabet("abcdefghijkmn124345678!@#$%^&*opqrstuvwxyz!*abcdefghijklmn"));
}
}