单词模式

给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。

这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。

示例1:

输入: pattern = "abba", str = "dog cat cat dog"
输出: true

示例 2:

输入:pattern = "abba", str = "dog cat cat fish"
输出: false

示例 3:

输入: pattern = "aaaa", str = "dog cat cat dog"
输出: false

示例 4:

输入: pattern = "abba", str = "dog dog dog dog"
输出: false

说明:
你可以假设 pattern 只包含小写字母, str 包含了由单个空格分隔的小写字母。    

 

https://leetcode-cn.com/problems/word-pattern/description/

 

class Solution {
    public boolean wordPattern(String pattern, String str) {
        char[] pc = pattern.toCharArray();
        String[] words = str.split(" ");
        //长度不等
        if (pc.length != words.length) {
            return false;
        }
        //以ascii码作为下标存储String。也可以String[] mark = new String[26];只要下面mark[pc[i]-97]即可
        String[] mark = new String[123];
        for (int i = 0; i < pc.length ; i++) {
            if (mark[pc[i]] == null) {
                mark[pc[i]] = words[i];
            } 
            //已有值,但与之前存储的不同,所以格式错误
            else if (!mark[pc[i]].equals(words[i])) { 
                return false;
            }
        }
        
        //判断mark数组中是否有重复值,有重复也表示格式不对
        
        // //方法1:利用hashSet
        // HashSet<String> set = new HashSet<>();
        // for (int i = 97; i < 123; i++){
        //     //add结果为false代表有重复
        //     if (mark[i] != null && (!set.add(mark[i]))) {
        //         return false;
        //     }
        // }
        
        //方法2:遍历两次
        for (int i = 97; i < 122; i++){
            for (int j = i + 1; j < 123; j++) {
                if (mark[i] != null && mark[i].equals(mark[j])) {
                    return false;
                }
            }
        }
        
        return true;
    }
}

//尝试先把String[]处理成int[],减少equals判断。
class Solution {
    public boolean wordPattern(String pattern, String str) {
        char[] pc = pattern.toCharArray();
        String[] words = str.split(" ");
        //长度不等
        if (pc.length != words.length) {
            return false;
        }
        int temp = 0;
        //以int数组代替String数组
        int[] wordsInt = new int[words.length];
        for (int i = 0; i < words.length; i++) {
            if (wordsInt[i] == 0) {
                temp++;
                //不同的String用不同的数字表示
                wordsInt[i] = temp;
                for (int j = i + 1; j < words.length; j++) {
                    //先判断wordsInt[j]是否已赋值了,减少equals判断
                    if (wordsInt[j] == 0 && words[i].equals(words[j])) {
                        wordsInt[j] = wordsInt[i];
                    }
                }
            }
        }
        //pattern不超过26种
        if (temp > 26) {
            return false;
        }
        //以wordsInt作为下标存储
        int[] mark = new int[27];
        for (int i = 0; i < pc.length; i++) {
            if (mark[wordsInt[i]] == 0) {
                mark[wordsInt[i]] = pc[i];
            }
            //已有值,但与之前存储的不同,所以格式错误
            else if (mark[wordsInt[i]] != pc[i]) {
                return false;
            }
        }
        for (int i = 1; i < 26; i++) {
            for (int j = i + 1; j < 27; j++) {
                if (mark[i] != 0 && mark[i] == mark[j]) {
                    return false;
                }
            }
        }
        return true;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值