当前位置:网站首页>Leetcode- find a difference - simple
Leetcode- find a difference - simple
2022-06-13 05:48:00 【AnWenRen】
title :389 Make a difference - Simple
subject
Given two strings s and t, They contain only lowercase letters .
character string *t* By string *s* Random rearrangement , Then add a letter at random .
Please find out in t The letters added in .
Example 1
Input :s = "abcd", t = "abcde"
Output :"e"
explain :'e' It's the letter that was added .
Example 2
Input :s = "", t = "y"
Output :"y"
Example 3
Input :s = "a", t = "aa"
Output :"a"
Example 4
Input :s = "ae", t = "aea"
Output :"a"
Tips
0 <= s.length <= 1000t.length == s.length + 1sandtContains only lowercase letters
Code Java
// Passed But it's slow 14ms
public char findTheDifference(String s, String t) {
if (s.length() == 0)
return t.charAt(0);
// 1 take s Elements in map to map in ,key Is element value ,value Yes, there are
// 2 take t Mapping to map in ,
// 3 If it does not exist Return this element directly , If there is, then value-1, When it comes to 0 when , It means that there is no , Return this element
HashMap map = new HashMap();
for (int i = 0; i < s.length(); i++) {
Object get = map.get(s.charAt(i));
if (get != null) {
int count = (int) get + 1;
map.put(s.charAt(i), count);
} else {
map.put(s.charAt(i), 1);
}
}
for (int i = 0; i < t.length(); i++) {
Object o = map.get(t.charAt(i));
if (o == null)
return t.charAt(i);
else {
int count = (int) o - 1;
if (count < 0)
return t.charAt(i);
map.put(t.charAt(i), count);
}
}
return 'F';
}
// Relatively fast 3ms
public char findTheDifference1(String s, String t) {
char[] chars = s.toCharArray();
char[] chart = t.toCharArray();
Arrays.sort(chars);
Arrays.sort(chart);
for (int i = 0; i < s.length(); i++) {
if (chars[i] != chart[i])
return chart[i];
}
return chart[s.length()];
}
// Exclusive or two sets of data 2ms
public char findTheDifference2(String s, String t) {
int sum = 0;
for (int i = 0; i < s.length(); i++) {
sum ^= s.charAt(i);
sum ^= t.charAt(i);
}
sum ^= t.charAt(s.length());
return (char) sum;
}
边栏推荐
- 2021.9.29学习日志-Restful架构
- 若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
- 2021.9.29 learning log restful architecture
- Bicolor case
- 零拷贝技术
- @Detailed explanation of propertysource usage method and operation principle mechanism
- Solutions to conflicts between xampp and VMware port 443
- MySQL table data modification
- Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
- 13 cancelendevent of a flowable end event and compensationthrowing of a compensation event
猜你喜欢

12 error end event and terminateendevent of end event

Application virtual directory static resource configuration on tongweb

Basic operations of MySQL auto correlation query

Source code analysis of ArrayList

arrayList && linkedList

Concurrent programming -- source code analysis of thread pool

2021.9.30 learning log -postman

Vagrant virtual machine installation, disk expansion and LAN access tutorial

Timeout thread log for tongweb

Shardingsphere JDBC exception: no table route info
随机推荐
Working principle of sentinel series (concept)
2021.9.29学习日志-Restful架构
The problem of distinguishing and sharing sessions for multiple applications in tongweb
MySQL fuzzy query and sorting by matching degree
JNDI configuration for tongweb7
Windbos common CMD (DOS) command set
Mongodb multi field aggregation group by
MySQL main query and sub query
How to Algorithm Evaluation Methods
Tongweb customs clearance guidelines
MySQL installation in Linux Environment
How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
顶部下滑沉浸式dialog
Mobile end adaptation scheme
10 signalstartevent and signalcatchingevent of flowable signal events
Etcd fast cluster building
11 signalthrowingevent and signalboundaryevent of flowable signal event
@Detailed explanation of propertysource usage method and operation principle mechanism
零拷贝技术
15 inclusivegateway and eventgateway of flowable gateway