当前位置:网站首页>Leetcode topic resolution valid anagram
Leetcode topic resolution valid anagram
2022-06-23 06:17:00 【ruochen】
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
Simulate a hash table with an array , Count the number of characters .
public boolean isAnagram(String s, String t) {
if (s == null || t == null) {
return false;
}
if (s.length() != t.length()) {
return false;
}
if (s.equals(t)) {
return false;
}
int len = s.length();
int[] map = new int[26];
// Statistics s The number of times each character appears in
for (int i = 0; i < len; i++) {
map[s.charAt(i) - 'a']++;
}
// subtract t Occurrence times of corresponding characters in
for (int i = 0; i < len; i++) {
map[t.charAt(i) - 'a']--;
if (map[t.charAt(i) - 'a'] < 0) {
return false;
}
}
for (int i = 0; i < 26; i++) {
if (map[i] != 0) {
return false;
}
}
return true;
}边栏推荐
- Extend your kubernetes API using the aggregation API
- Microsoft interview question: creases in origami printing
- Radar canvas
- jvm-01. Instruction rearrangement
- mysql读已提交和可重复度区别
- Find the number of nodes in the widest layer of a binary tree
- Basic calculator II for leetcode topic analysis
- Vant web app calendar component performance optimization calendar add min date the minimum date page loads slowly
- Multiple strings for leetcode topic resolution
- (1)基础学习——vim编辑器常用快捷操作命令
猜你喜欢

Radar canvas

(1)基础学习——vim编辑器常用快捷操作命令

金融科技之高效办公(一):自动生成信托计划说明书

Ant Usage Summary (III): batch packaging apk

100-300 cases of single chip microcomputer program (detailed explanation of notes)

Jour 13 Projet de santé mentale - chapitre 13

Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql

jvm-06. Garbage collector

Pyqt5 setting window top left Icon

Visual Studio调试技巧
随机推荐
Day_ 13 smart health project - Chapter 13
Wireshark TS | 视频 APP 无法播放问题
11、 Realization of textile fabric off shelf function
Pyinstaller package exe setting icon is not displayed
Pat class B 1018 C language
mongodb 4. X binding multiple IP startup errors
Pyinstaller packaging pyttsx3 error
学习太极创客 — ESP8226 (十一)用 WiFiManager 库配网
【Cocos2d-x】可擦除的Layer:ErasableLayer
Difference between MySQL read committed and repeatability
Pat class B 1025 reverse linked list
jvm-04.对象的内存布局
Day_12 传智健康项目-JasperReports
Pyqt5 设置窗口左上角图标
Pat class B 1023 minimum decimals
100-300 cases of single chip microcomputer program (detailed explanation of notes)
Progress of layer 2 technical scheme
Possible pits in mongodb project
Kotlin collaboration +retro most elegant network request use
Wireshark TS | video app cannot play