-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17.java
More file actions
51 lines (50 loc) · 1.45 KB
/
day17.java
File metadata and controls
51 lines (50 loc) · 1.45 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
// Question_1 :
// 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 c1 = s.charAt(i);
char c2 = t.charAt(i);
if (map1.containsKey(c1)) {
if (map1.get(c1) != c2) return false;
}
if (map2.containsKey(c2)) {
if (map2.get(c2) != c1) return false;
}
map1.put(c1, c2);
map2.put(c2, c1);
}
return true;
}
}
TC-O(n)
SC-O(n)
// Question_2 :
// https://leetcode.com/problems/remove-outermost-parentheses/description/
class Solution {
public String removeOuterParentheses(String s) {
Stack<Character> st=new Stack<>();
StringBuilder str=new StringBuilder();
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
if(ch=='('){
if(!st.isEmpty()){
str.append('(');
}
st.push(ch);
}
else{
st.pop();
if(!st.isEmpty()){
str.append(')');
}
}
}
return str.toString();
}
}
TC-O(n)
SC-O(n)