【4】Decide if two strings are anagrams or not
来源:互联网 发布:js跨域修改iframe样式 编辑:程序博客网 时间:2024/06/05 23:01
Question: Write a method to decide if two strings are anagrams or not.
package CareerCup;public class Anagram {public Anagram(){}public boolean detect(String str1, String str2){if(str1.length()!=str2.length()) return false;int[] chars = new int[256];int NumUniqueChar = 0;int Num=0;for(int i=0;i<str1.length();i++){int ch = str1.charAt(i);if(chars[ch]==0) NumUniqueChar++;chars[ch]++;}for(int i=0;i<str2.length();i++){if(chars[str2.charAt(i)]==0) return false;chars[str2.charAt(i)]--;if(chars[str2.charAt(i)]==0){Num++;if(Num==NumUniqueChar)return i==str2.length()-1;}}return false;}public static void main(String[] args){String str1 = "abcdef";String str2 = "adbgef";System.out.print(str1+" and "+ str2);Anagram agr = new Anagram();if(agr.detect(str1,str2))System.out.print(" are anagrams!");else System.out.print(" are not anagrams!");System.out.println();}}
- 【4】Decide if two strings are anagrams or not
- [CrackCode] 1.4 Write a method to decide if two strings are anagrams or not
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams-LintCode
- #158 Two Strings Are Anagrams
- [Amazon] Two Strings Are Anagrams (Compare Strings)
- Lintcode158 Two Strings Are Anagrams solution 题解
- LintCode Two Strings Are Anagrams 两个字符串是变位词
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- Given two binary trees, write a function to check if they are equal or not.
- 【6】Decide if one string is a rotation of another string or not
- Two Strings are Anagram
- 158.Two Strings Are Anagrams-两个字符串是变位词(容易题)
- The only legal comparisons are between two numbers, two strings, or two dates
- How to Determine if Two Words Are Anagrams of Each Other in C# (转)
- hdu 3664 递推
- 从HTML原型到jsp页面完美转型攻略(教你即使不会写代码也能弄出漂亮的网页)
- 使用Eclipse重构代码——Replace Temp with Query
- ortp库使用入门
- MFC 制作OCX插件
- 【4】Decide if two strings are anagrams or not
- POJ3356 AGTC
- Cookie详细介绍
- ubuntu 使用命令行将txt转换为pdf
- java.lang.UnsupportedClassVersionError: Bad version number in .class file
- 第15天的交易(2013-4-10)(-100) (-1125) (黑色的一天)
- iOS6下Objective-C最新特性
- 实验三 继承,上转型对象 之Example.java
- hdu 1867 A + B for you again