码迷,mamicode.com
首页 > 其他好文 > 详细

Lintcode: Two Strings Are Anagrams

时间:2015-04-15 07:12:54      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

Write a method anagram(s,t) to decide if two strings are anagrams or not.

Example
Given s="abcd", t="dcab", return true

O(N)time, O(1) space

 1 public class Solution {
 2     /**
 3      * @param s: The first string
 4      * @param b: The second string
 5      * @return true or false
 6      */
 7     public boolean anagram(String s, String t) {
 8         // write your code here
 9         if (s==null || t==null || s.length()!=t.length()) return false;
10         int[] ss = new int[256];
11         int[] tt = new int[256];
12         for (int i=0; i<s.length(); i++) {
13             ss[s.charAt(i)]++;
14             tt[t.charAt(i)]++;
15         }
16         for (int j=0; j<256; j++) {
17             if (ss[j] != tt[j]) return false;
18         }
19         return true;
20     }
21 };

 

Lintcode: Two Strings Are Anagrams

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/4427467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!