Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
76 lines (71 loc) · 2.71 KB

File metadata and controls

76 lines (71 loc) · 2.71 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package com.problems;
import java.util.*;
public class StringProblemsOne {
// shortest substring of 's' that has all the characters from string 't'
public String controlSet(String s, String t){
// T(n) : o(n^2)
int[] result = {-1, -1};
int shortest = Integer.MAX_VALUE;
Set<Character> set = new HashSet<>();
for(int i = 0; i < t.length(); i++){
set.add(t.charAt(i));
}
int left = 0;
for(int i = 0; i < s.length(); i++){
Map<Character, Integer> map = new HashMap<>();
for(int j = i; j < s.length(); j++){
//System.out.println(s.substring(i, j+1));
if(set.contains(s.charAt(j))){
map.put(s.charAt(j), map.getOrDefault(s.charAt(j), 0) + 1);
}
if(map.size() == set.size() && j - i + 1 < shortest){
//shortest = Math.min(shortest, j - i + 1);
shortest = j - i + 1;
result[0] = i;
result[1] = j+1;
}
}
}
return s.substring(result[0], result[1]);
}
public String controlSetSlidingWindow(String s, String t){
// T(n) : O(n)
int[] result = {-1, -1};
int shortest = Integer.MAX_VALUE;
Set<Character> set = new HashSet<>();
for(int i = 0; i < t.length(); i++){
set.add(t.charAt(i));
}
int left = 0;
Map<Character, Integer> map = new HashMap<>();
for(int i = 0; i < s.length(); i++){
if(set.contains(s.charAt(i))){
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
}
while (map.size() >= set.size()){
map.put(s.charAt(left), map.get(s.charAt(left)) - 1);
if(map.get(s.charAt(left)) == 0){
map.remove(s.charAt(left));
}
left++;
}
//left--;
//map.put(s.charAt(left), map.getOrDefault(s.charAt(left), 0) + 1);
if(map.size() == set.size() && i - left + 1 < shortest){
shortest = i - left + 1;
result[0] = left;
result[1] = i;
}
}
return s.substring(result[0], result[1]);
}
}
class TDStringProblemsOne{
public static void main(String[] args) {
StringProblemsOne obj = new StringProblemsOne();
System.out.println(obj.controlSet("helloworld", "lrw"));
System.out.println(obj.controlSet("helloworwld", "lrw"));
System.out.println(obj.controlSetSlidingWindow("helloworld", "lrw"));
System.out.println(obj.controlSetSlidingWindow("helloworwld", "lrw"));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.