-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay18.java
More file actions
49 lines (47 loc) · 1.16 KB
/
Copy pathDay18.java
File metadata and controls
49 lines (47 loc) · 1.16 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
//Problem:Longest Common Prfefix
//https://leetcode.com/problems/longest-common-prefix/
class Solution {
public String longestCommonPrefix(String[] strs) {
StringBuilder sb=new StringBuilder();
Arrays.sort(strs);
char[] first=strs[0].toCharArray();
char[] last=strs[strs.length-1].toCharArray();
int i=0;
while(i<first.length && i<last.length)
if(first[i]==last[i]){
sb.append(first[i]);
i++;
}
else{
break;
}
return sb.toString();
}
}
//TC:O(nlogn+m)
//SC:O(m)
//Problem:Valid Anagram
//https://leetcode.com/problems/valid-anagram/
class Solution {
public boolean isAnagram(String s, String t) {
HashMap<Character,Integer> map=new HashMap<>();
int i=0;
if (s.length() != t.length()) return false;
for (char ch : s.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
while(i<t.length()){
if(map.containsKey(t.charAt(i)))
map.put(t.charAt(i),map.get(t.charAt(i))-1);
else return false;
i++;
}
for(int value:map.values()){
if(value!=0)
return false;
}
return true;
}
}
//TC:o(n)
//SC:o(n)