-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17.java
More file actions
62 lines (46 loc) · 1.58 KB
/
Copy pathday17.java
File metadata and controls
62 lines (46 loc) · 1.58 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
//ques1:205. Isomorphic Strings
//link:https://leetcode.com/problems/isomorphic-strings/description/
class Solution {
public boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) return false;
HashMap<Character, Character> map1 = new HashMap<>();
HashMap<Character, Character> map2= new HashMap<>();
for(int i = 0; i < s.length(); i++) {
char ch1 = s.charAt(i);
char ch2 = t.charAt(i);
if(map1.containsKey(ch1) && map1.get(ch1) != ch2) return false;
if(map2.containsKey(ch2) && map2.get(ch2) != ch1) return false;
map1.put(ch1, ch2);
map2.put(ch2, ch1);
}
return true;
}
}
//TC:O(n)
//SC:O(n)
//ques2:1021. Remove Outermost Parentheses
//link:https://leetcode.com/problems/remove-outermost-parentheses/description/
import java.util.*;
class Solution2 {
public String removeOuterParentheses(String s) {
List<Integer> li = new ArrayList<>();
Stack<Character> st = new Stack<>();
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
if (!st.isEmpty()) {
result.append(s.charAt(i));
}
st.push(s.charAt(i));
} else if (s.charAt(i) == ')') {
st.pop();
if (!st.isEmpty()) {
result.append(s.charAt(i));
}
}
}
return result.toString();
}
}
//TC:O(N)
//SC:O(N)