当前位置:网站首页>[leetcode] day96 - the first unique character & ransom letter & letter ectopic word
[leetcode] day96 - the first unique character & ransom letter & letter ectopic word
2022-07-06 05:57:00 【Upside down, it's a circle】
1. The first unique character in the string
387. The first unique character in the string 【 Simple 】
Answer key
Storage frequency using hash table , Here the hash table is used HashMap
class Solution {
public int firstUniqChar(String s) {
Map<Character,Integer>hashMap=new HashMap<>();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(hashMap.containsKey(c)){
int count=hashMap.get(c);
hashMap.put(c,count+1);
}
else
hashMap.put(c,1);
}
for(int i=0;i<s.length();i++){
if(hashMap.get(s.charAt(i))==1)
return i;
}
return -1;
}
}
Time complexity : O ( n ) O(n) O(n)
Spatial complexity : O ( ∣ Σ ∣ ) O(∣Σ∣) O(∣Σ∣), among Σ \Sigma Σ It's a character set.
2. Ransom letter
Answer key
Storage frequency using hash table , Here the hash table is used Array , Less time to run
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] cnt=new int[26];
for(int i=0;i<magazine.length();i++){
cnt[magazine.charAt(i)-'a']++;
}
for(int i=0;i<ransomNote.length();i++){
char c=ransomNote.charAt(i);
if(cnt[c-'a']<=0)
return false;
cnt[c-'a']--;
}
return true;
}
}
Time complexity : O ( m + n ) O(m+n) O(m+n)
Spatial complexity : O ( ∣ Σ ∣ ) O(∣Σ∣) O(∣Σ∣), among Σ \Sigma Σ It's a character set.
3. Effective alphabetic words
242. Effective alphabetic words 【 Simple 】
Answer key
Hashtable
class Solution {
public boolean isAnagram(String s, String t) {
int m=s.length(),n=t.length();
if(m!=n)
return false;
int[] snt=new int[26];
for(int i=0;i<m;i++)
snt[s.charAt(i)-'a']++;
for(int i=0;i<n;i++){
snt[t.charAt(i)-'a']--;
if(snt[t.charAt(i)-'a']<0)
return false;
}
return true;
}
}
Be careful : Mentioned in the advanced “ If the input string contains unicode character What do I do ”?
In this case, the hash table cannot be represented by an array , and Only use HashMap!
Time complexity : O ( n ) O(n) O(n)
Spatial complexity : O ( ∣ Σ ∣ ) O(∣Σ∣) O(∣Σ∣), among Σ \Sigma Σ It's a character set.
Sort
t yes s The heterotopic words of are equivalent to 「 Two strings are sorted equal 」. So we can do string s and t Sort them separately , You can judge whether the sorted strings are equal .
Just remember String to array 、 Sort 、 Sentence etc. The operation of API
class Solution {
public boolean isAnagram(String s, String t) {
int m=s.length(),n=t.length();
if(m!=n)
return false;
char[] str1=s.toCharArray();
char[] str2=t.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
return Arrays.equals(str1,str2);
}
}
Time complexity : O ( n l o g n ) O(nlogn) O(nlogn), Sort required
Spatial complexity : O ( l o g n ) O(logn) O(logn), Sort required
边栏推荐
- Leetcode 701 insertion operation in binary search tree -- recursive method and iterative method
- Station B, Master Liu Er - dataset and data loading
- IPv6 comprehensive experiment
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Introduction to promql of # yyds dry goods inventory # Prometheus
- Download, install and use NVM of node, and related use of node and NRM
- Implementation of linked list in address book management system
- Jushan database appears again in the gold fair to jointly build a new era of digital economy
- What is independent IP and how about independent IP host?
- Garbage collector with serial, throughput priority and response time priority
猜你喜欢
Clock in during winter vacation
[SQL Server fast track] - authentication and establishment and management of user accounts
Clear floating mode
High quality coding tool clion
Li Chuang EDA learning notes 12: common PCB board layout constraint principles
[Tang Laoshi] C -- encapsulation: classes and objects
Database: ODBC remote access SQL Server2008 in oracel
Station B, Master Liu Er - back propagation
Leetcode 701 insertion operation in binary search tree -- recursive method and iterative method
Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
随机推荐
A master in the field of software architecture -- Reading Notes of the beauty of Architecture
授予渔,从0开始搭建一个自己想要的网页
養了只小猫咪
Station B Liu Erden - linear regression and gradient descent
B站刘二大人-反向传播
Clear floating mode
[Jiudu OJ 08] simple search x
嵌入式面试题(一:进程与线程)
Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
[Thesis code] SML part code reading
OSPF configuration command of Huawei equipment
As3013 fire endurance test of cable distribution system
Summary of data sets in intrusion detection field
入侵检测领域数据集总结
[imgui] unity MenuItem shortcut key
【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
[experience] install Visio on win11
【无标题】
Station B, Mr. Liu Er - multiple logistic regression, structure 7
Network protocol model