首页 > 其他 > 详细

Lintcode: Two Strings Are Anagrams

时间:2015-04-15 07:10:44      阅读:306      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!