[LintCode]Two Strings Are Anagrams(C++|Java|Python)
来源:互联网 发布:淘宝比较靠谱的韩代 编辑:程序博客网 时间:2024/05/18 03:48
C++ Version:
class Solution {public: /** * @param s: The first string * @param b: The second string * @return true or false */ bool anagram(string s, string t) { // write your code here int count[128][2] = {0}; if (s.length() != t.length()) { return false; } for (int i = 0; i < s.length(); i++) { count[s[i]][0]++; count[t[i]][1]++; } for (int i = 0; i < 128; i++) { if (count[i][0] != count[i][1]) { return false; } } return true; }};
Java Version:
public class Solution { /** * @param s: The first string * @param b: The second string * @return true or false */ public boolean anagram(String s, String t) { // write your code here char[] a = s.toCharArray(); char[] b = t.toCharArray(); int i = 0; int[][] count = new int[128][2]; if (a.length != b.length){ return false; } for (i = 0; i < a.length; i++){ count[Integer.valueOf(a[i])][0]++; count[Integer.valueOf(b[i])][1]++; } for (i = 0; i < 128; i++){ if (count[i][0] != count[i][1]){ return false; } } return true; }};
Python Version:
class Solution: """ @param s: The first string @param b: The second string @return true or false """ def anagram(self, s, t): # write your code here if len(s) != len(t): return False for c in s: if t.count(c) != s.count(c): return False return True
阅读全文
0 0
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- Two Strings Are Anagrams-LintCode
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- LintCode Two Strings Are Anagrams 两个字符串是变位词
- #158 Two Strings Are Anagrams
- [Amazon] Two Strings Are Anagrams (Compare Strings)
- Lintcode158 Two Strings Are Anagrams solution 题解
- 【4】Decide if two strings are anagrams or not
- [LintCode]Compare Strings(C++|Python)
- Two Strings are Anagram
- [CrackCode] 1.4 Write a method to decide if two strings are anagrams or not
- 158.Two Strings Are Anagrams-两个字符串是变位词(容易题)
- [LintCode]Anagrams(Python)
- 762C - Two strings
- C.Two strings
- 正则表达式
- static_cast、dynamic_cast、const_cast和reinterpret_cast总结
- 基于vue-cli的Webpack构建
- Spring cloud 学习(一)
- (五)Angular4 英雄征途HeroConquest-多组件开发
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- ruby on rails 里pluck和select(map、collect)的区别
- 二维码用途分类
- linux系统排错与修复
- java
- hdu 6038 Function 循环节
- sed命令中的正则表达式问号、加号、圆括号等需要转义
- c++ 类模板 用栈实现队列
- 欢迎使用CSDN-markdown编辑器