当前位置:网站首页>Leetcode 242. valid anagram
Leetcode 242. valid anagram
2022-07-29 02:09:00 【Blue feather birds】
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “anagram”, t = “nagaram”
Output: true
Example 2:
Input: s = “rat”, t = “car”
Output: false
Give the string s, t, Judge t Is it right? s The heterotopic words of .
That's judgment t Is the letter of s Rearrangement of letters in , Use each letter only once .
Ideas :
t yes s Rearrangement of letters in , And each letter is only used once ; Means t Only use s Letters in , And only once ,
that s and t The length of must be equal , It must be false.
No matter how it is arranged , First of all, make sure s and t The number of each letter in must be consistent , If the number is inconsistent s No matter how you arrange it, it won't become t.
Then you only need to count the number of each letter , Then judge whether the number of each letter is consistent .
As for the details s How to arrange to get t , Don't care about this topic .
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
int[] sc = new int[26];
int[] tc = new int[26];
for(int i = 0; i < s.length(); i++) {
sc[s.charAt(i) - 'a'] ++;
tc[t.charAt(i) - 'a'] ++;
}
for(int i = 0; i < 26; i++) {
if(sc[i] != tc[i]) return false;
}
return true;
}
边栏推荐
- Force deduction brush question (2): sum of three numbers
- 数学建模——永冻土层上关于路基热传导问题
- Large scale web crawling of e-commerce websites (Ultimate Guide)
- 解决使用ESlint时,和vetur冲突导致保存变双引号,结尾逗号等
- Leetcode/0 and 1 consecutive subarrays with the same number
- What is browser fingerprint recognition
- Lua log implementation -- print table
- FPGA实现10M多功能信号发生器
- druid. io index_ Realtime real-time query
- Process -- user address space and kernel address space
猜你喜欢

Mathematical modeling -- Optimization of picking in warehouse

druid. io kill -9 index_ Realtime traceability task

数学建模——永冻土层上关于路基热传导问题

Flexible layout single selection

IDEA 连接 数据库

【流放之路-第八章】

Overview of Qualcomm 5g intelligent platform

Comprehensive explanation of "search engine crawl"

Large scale web crawling of e-commerce websites (Ultimate Guide)

Data platform data access practice
随机推荐
Explanation of yocto project directory structure
[the road of Exile - Chapter 4]
druid. IO custom real-time task scheduling policy
Leetcode 113: path sum II
【流放之路-第四章】
How companies make business decisions -- with the help of data-driven marketing
[MySQL] SQL aliases the table
数学建模——永冻土层上关于路基热传导问题
mobile-picker.js
Have you ever encountered the situation that the IP is blocked when crawling web pages?
Use of packet capturing tool Charles
Leetcode 112: path sum
Planning mathematics final exam simulation II
[7.21-26] code source - [square count] [dictionary order minimum] [Z-type matrix]
h5背景音乐通过触摸自动播放
Lua third-party byte stream serialization and deserialization module --lpack
The solution of reducing the sharpness of pictures after inserting into word documents
分布式开发漫谈
点击回到顶部js
[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform