Given two strings s and t, write a function to determine if t is an anagram of s.

本文介绍了一种通过排序和哈希映射两种方法来判断两个字符串是否构成字谜(anagram)的有效算法。首先检查两个字符串长度是否相等,然后使用字符排序比较或哈希表计数的方式确定它们是否由相同的字符组成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:

You may assume the string contains only lowercase alphabets.


=================================

public class Solution {
     public boolean isAnagram(String s, String t) {
        if(s == null && t == null) {
            return true;
        }
        if((s==null&&t!=null )||(s!=null&&t==null)||(s.length()!=t.length())){
            return  false;
        }


        char[] arrs = s.toCharArray();
        char[] arrt = t.toCharArray();


        Arrays.sort(arrs);
        Arrays.sort(arrt);


        for(int i = 0; i < arrs.length; i++) {
            if(arrs[i] != arrt[i]) {
                return false;
            }
        }
        return true;
    }
}
方法二:
=========================hash Map=========================
import java.util.*;
public class Solution {
       public boolean isAnagram(String s, String t) {
       /* if(s==null||t==null||s.length()!=t.length()){
            return  false;
        }*/
        if(s == null && t == null) {
            return true;
        }
        if((s==null&&t!=null )||(s!=null&&t==null)||(s.length()!=t.length())){
            return  false;
        }
        int a[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int b[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
//        char[] str = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        HashMap<Integer, String> hm = new HashMap<Integer, String>();
        hm.put(1, "a");
        hm.put(2, "b");
        hm.put(3, "c");
        hm.put(4, "d");
        hm.put(5, "e");
        hm.put(6, "f");
        hm.put(7, "g");
        hm.put(8, "h");
        hm.put(9, "i");
        hm.put(10, "j");
        hm.put(11, "k");
        hm.put(12, "l");
        hm.put(13, "m");
        hm.put(14, "n");
        hm.put(15, "o");
        hm.put(16, "p");
        hm.put(17, "q");
        hm.put(18, "r");
        hm.put(19, "s");
        hm.put(20, "t");
        hm.put(21, "u");
        hm.put(22, "v");
        hm.put(23, "w");
        hm.put(24, "x");
        hm.put(25, "y");
        hm.put(26, "z");
        Collection<Integer> c2 = hm.keySet();
        for (int i = 0; i < s.length(); i++) {
            for (Integer intr : c2) {
        /*
        * 比如 char a='4'
         String b=a+"";
         String c=String.valueOf(a);
        * */
       if (String.valueOf(s.charAt(i)).equals(hm.get(intr))) {
                    a[intr-1] += 1;
                    break;
                }
            }


        }
        for (int j = 0; j < t.length(); j++) {
            for (Integer key : c2) {
                if (String.valueOf(t.charAt(j)).equals(hm.get(key))) {
                    b[key-1] += 1;
                    break;
                }
            }


        }
        for(int k=0;k<a.length;k++){
            if(a[k]!=b[k]){
                return  false;
            }
        }
        return true;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漁陽

彼此共勉,砥砺前行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值