题目:
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?

分析:
题意是给定2个字符串,判断这2个字符串是否由相同的字符组成,并且对应字符的个数一致。

代码实现(时间复杂度O(n^2)):

public class Solution {public boolean isAnagram(String s, String t) {char[] sarr=s.toCharArray();char[] tarr=t.toCharArray();if(sarr.length!=tarr.length){return false;}for(int i=0;i<sarr.length;i++){boolean found=false;for(int j=i;j<sarr.length;j++){if(sarr[i]==tarr[j]){found=true;if(i!=j){tarr[j]=tarr[i];tarr[i]=sarr[i];}break;}}if(found==false){return false;}}return true;}
}

Leet Code OJ 242. Valid Anagram [Difficulty: Easy]相关推荐

  1. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  2. Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]

    题目: Given a string containing just the characters , determine if the input string is valid. The brac ...

  3. Leet Code OJ 112. Path Sum [Difficulty: Easy]

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  4. Leet Code OJ 344. Reverse String [Difficulty: Easy]

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  5. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  6. Leet Code OJ 1. Two Sum [Difficulty: Easy]

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. Leet Code OJ 223. Rectangle Area [Difficulty: Easy]

    题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...

  8. Leet Code OJ 189. Rotate Array [Difficulty: Easy]

    题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...

  9. Leet Code OJ 66. Plus One [Difficulty: Easy]

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

最新文章

  1. MIT喊你来上课,深度学习课程,免费的那种 | 资源
  2. 关于批量插入数据之我见(100万级别的数据,mysql) (转)
  3. 赶紧下载SublimeText并快速设置代码自动补全,效率大大提高
  4. 中商惠民签约神策数据 致力为客户提供最优服务模式
  5. 容器化时代我们应当选择Kubernetes
  6. http的“无连接”指的是_http协议无状态中的 quot;状态quot; 到底指的是什么?...
  7. 数据库MySQL相关操作||创建数据库、显示所有数据库、切换数据库、显示数据库下的数据库表、删除数据库
  8. 通信工程中常用算法c语言,通信工程常见面试题.doc
  9. CVPR 2021 底层视觉最新进展分享
  10. 监理延期 验收工程款制约三方
  11. Android与Chromium源码搜索工具
  12. python--multiprocessing多进程总结
  13. python载入图片序列_python – 使用matplotlib显示图像序列
  14. Axure 8.1.0.3382 激活码(转)
  15. 音频处理——音频编码原理简介
  16. 浅谈什么是大数据?大数据可以做什么?
  17. 如何快速批量修改图片名称?
  18. 狄利克雷卷积_算法学习笔记(35): 狄利克雷卷积
  19. 航天金盾人脸识别系统解决方案
  20. python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐

热门文章

  1. Fib数模n的循环节
  2. Win10任务计划+PowerShell实现Git自动推送
  3. SkinMagic使用后按钮加自定义图标或菜单GetMneu返回NULL的解决方法
  4. 面试被问 | 防止 Java 代码被反编译的方法有几种?
  5. 网络编程套接字(四)
  6. 对话七牛云技术总监陈辉:音视频行业中的新系统新规划和新增长
  7. 测试:将手机摄像头用作网络摄像头的拍摄效果
  8. 淘系的音视频编辑方案:非线性编辑引擎
  9. 用FFmpeg搭建基于CNN的视频分析方案
  10. 华人的战场——MSU视频编码大赛