当前位置:网站首页>[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
边栏推荐
- (column 22) typical column questions of C language: delete the specified letters in the string.
- AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
- Web service connector: Servlet
- What preparations should be made for website server migration?
- LTE CSFB process
- B站刘二大人-多元逻辑回归 Lecture 7
- Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
- The usage and difference between strlen and sizeof
- 清除浮动的方式
- [string] palindrome string of codeup
猜你喜欢

如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
![[Jiudu OJ 08] simple search x](/img/a7/12a00c5d1db2deb064ff5f2e83dc58.jpg)
[Jiudu OJ 08] simple search x

Yunxiaoduo software internal test distribution test platform description document

Novice entry SCM must understand those things

The usage and difference between strlen and sizeof

Mysql database master-slave cluster construction

Installation de la Bibliothèque de processus PDK - csmc

【课程笔记】编译原理

Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง

Embedded interview questions (IV. common algorithms)
随机推荐
Rustdesk builds its own remote desktop relay server
Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
How can large websites choose better virtual machine service providers?
ArcGIS application foundation 4 thematic map making
Redistemplate common collection instructions opsforvalue (II)
Mysql database master-slave cluster construction
局域网同一个网段通信过程
P2802 go home
[paper reading] nflowjs: synthetic negative data intensive anomaly detection based on robust learning
Station B Liu Erden softmx classifier and MNIST implementation -structure 9
华为路由器忘记密码怎么恢复
Go language -- language constants
Hongliao Technology: Liu qiangdong's "heavy hand"
How to use the container reflection method encapsulated by thinkphp5.1 in business code
网络协议模型
OSPF configuration command of Huawei equipment
Station B, Master Liu Er - dataset and data loading
Huawei BFD configuration specification
Auto.js学习笔记17:基础监听事件和UI简单的点击事件操作
Is it difficult for an information system project manager?