Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
来源:互联网 发布:mac版pdf如何修改文字 编辑:程序博客网 时间:2024/05/02 02:47
题目:
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; }}
1 0
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
- 【Leet Code】 242. Valid Anagram--easy
- Leet Code OJ 292. Nim Game [Difficulty: Easy]
- Leet Code OJ 231. Power of Two [Difficulty: Easy]
- Leet Code OJ 258. Add Digits [Difficulty: Easy]
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
- Leet Code OJ 283. Move Zeroes [Difficulty: Easy]
- Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]
- Leet Code OJ 169. Majority Element [Difficulty: Easy]
- Leet Code OJ 263. Ugly Number [Difficulty: Easy]
- Leet Code OJ 100. Same Tree [Difficulty: Easy]
- Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
- Leet Code OJ 202. Happy Number [Difficulty: Easy]
- Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
- Qt获得焦点和失去焦点处理事件 (Focus事件)
- hdu 2883 kebab(最大流dinic邻接表)
- android手机号码归属地免费api接口(有次数限制一般)
- Unity 5 中的全局光照技术详解
- 内存溢出问题详解
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
- kafka集群环境搭建
- 基于NSAttributedString与正则表达式实现的图文混排
- silverlight 遇到的问题
- C++Primer第5版读书笔记(第3章)
- Qt遍历控件
- cocos js 实现 滚动字幕 且自动根据文本的宽度稳定滚动速率
- 使用 linux-dnw 给S5PV210烧录裸机程序
- 刚装好的linux系统如何获取root的权限