当前位置:网站首页>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;
}
边栏推荐
- 数学建模——公交调度优化
- LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
- E-commerce keyword research helps data collection
- [circuit design] convert AC AC to DC
- 移动通信——基于卷积码的差错控制系统仿真模型
- Thirty years of MPEG audio coding
- Yocto project download and compilation
- leetcode/乘积小于K 的连续子数组的个数
- Sharpness evaluation method without reference image
- Mathematical modeling -- Optimization of picking in warehouse
猜你喜欢

Blind separation of speech signals based on ICA and DL

【云原生与5G】微服务加持5G核心网
![[UE4] replay game playback for ue4.26](/img/c3/1c7b30797f46dbd323cac4d158600f.png)
[UE4] replay game playback for ue4.26

【流放之路-第八章】
![[golang] network connection net.dial](/img/8d/7ef64cb63cbd230e5ac1655b86786f.png)
[golang] network connection net.dial

【流放之路-第二章】

【流放之路-第六章】

Talk about possible problems when using transactions (@transactional)

12.< tag-动态规划和子序列, 子数组>lt.72. 编辑距离

Lua log implementation -- print table
随机推荐
iVX低代码平台系列详解 -- 概述篇(二)
Mysql存储json格式数据
Mathematical modeling -- cold proof simulation of low temperature protective clothing with phase change materials
使用本地缓存+全局缓存实现小型系统用户权限管理
【云原生与5G】微服务加持5G核心网
Stonedb invites you to participate in the open source community monthly meeting!
Mathematical modeling -- bus scheduling optimization
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen
JS timer setinterval clearinterval delayer setTimeout asynchronous animation
Implementation of 10m multifunctional signal generator with FPGA
Monadic linear function perceptron: Rosenblatt perceptron
Comprehensive analysis of news capture doorway
Promise解决异步
[7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]
MySQL high performance optimization notes (including 578 pages of notes PDF document), collected
Mathematical modeling -- heat conduction of subgrade on Permafrost
给LaTeX公式添加优美的注解;日更『数据科学』面试题集锦;大学生『计算机』自学指南;个人防火墙;前沿资料/论文 | ShowMeAI资讯日报
12.< tag-动态规划和子序列, 子数组>lt.72. 编辑距离
Mathematical modeling -- the laying of water pipes
移动通信——基于卷积码的差错控制系统仿真模型