【力扣】面试经典150题——哈希表

文章目录

  • [383. 赎金信](#383. 赎金信)
  • [205. 同构字符串](#205. 同构字符串)
  • [290. 单词规律](#290. 单词规律)

383. 赎金信

给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。

如果可以,返回 true ;否则返回 false 。

magazine 中的每个字符只能在 ransomNote 中使用一次。

java 复制代码
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if(ransomNote.length() > magazine.length()){
            return false;
        }

        int[] cnt = new int[26];
        for(char c : magazine.toCharArray()){
            cnt[c - 'a']++;
        }
        for(char c : ransomNote.toCharArray()){
            cnt[c - 'a']--;
            if(cnt[c - 'a'] < 0){
                return false;
            }
        }
        return true;
    }
}

205. 同构字符串

给定两个字符串 s 和 t ,判断它们是否是同构的。

如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。

每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。

java 复制代码
class Solution {
    public boolean isIsomorphic(String s, String t) {
        Map<Character, Character> s2t = new HashMap<Character, Character>();
        Map<Character, Character> t2s = new HashMap<Character, Character>();

        int len = s.length();
        for(int i = 0; i < len; i++){
            char x = s.charAt(i), y = t.charAt(i);
            if((s2t.containsKey(x) && s2t.get(x) != y) || (t2s.containsKey(y) && t2s.get(y) != x)){  //当字符重复时,查看由键所得的值是否匹配
                return false;
            }
            s2t.put(x, y);
            t2s.put(y, x);
        }
        return true;

    }
}

290. 单词规律

给定一种规律 pattern 和一个字符串 s ,判断 s 是否遵循相同的规律。

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

java 复制代码
class Solution {
    public boolean wordPattern(String pattern, String str) {
        Map<String, Character> str2ch = new HashMap<String, Character>();
        Map<Character, String> ch2str = new HashMap<Character, String>();

        int m = str.length();
        int i = 0;
        for(int p = 0; p < pattern.length(); ++p){
            char ch = pattern.charAt(p);
            if(i >= m){
                return false;
            }
            int j = i;
            while(j < m && str.charAt(j) != ' '){
                j++;
            }
            String tmp = str.substring(i, j);
            if(str2ch.containsKey(tmp) && str2ch.get(tmp) != ch){
                return false;
            }
            if(ch2str.containsKey(ch) && !tmp.equals(ch2str.get(ch))){
                return false;
            }
            str2ch.put(tmp, ch);
            ch2str.put(ch, tmp);
            i = j + 1;
        }
        return i >= m;
    }
}

相关推荐
alphaTao4 小时前
LeetCode 每日一题 2026/9/14-2026/9/20
算法·leetcode
hanlin034 小时前
刷题笔记:力扣第560题-和为k的子数组
笔记·算法·leetcode
晚安日记wanna4 小时前
React 为何不做双端 diff?Vue 与 React 更新逻辑的三层差异
前端·react.js·面试
keke.shengfengpolang5 小时前
2026年资金分析岗校招技术栈拆解:Excel预测模型、SQL取数、SAP与Oracle资金模块,附2027届备考路线
面试
顶点多余5 小时前
9.16 面试总结
linux·面试·职场和发展
Navigator_Z16 小时前
LeetCode //C - 1252. Cells with Odd Values in a Matrix
c语言·算法·leetcode
Alice-YUE16 小时前
工业级 AI Agent 架构:5 层、2 范式、4 原则,一次讲透
面试·系统架构·大模型·ai agent·智能体
语戚18 小时前
力扣 1621. 大小为K的不重叠线段的数目:动态规划(Java 实现)
java·算法·leetcode·动态规划·力扣·dp
青山木18 小时前
Hot 100 --- 最长有效括号
java·数据结构·算法·leetcode·动态规划
这料鬼有毒19 小时前
二刷hot100-1143.最长公共子序列
算法·leetcode·动态规划