• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

Lintcode: Two Strings Are Anagrams

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 };

 

posted @ 2015-04-15 04:08  neverlandly  阅读(950)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3