
目录
1. 同构字符串 🌟
2. 随机字符串 🌟
3. 交错字符串 🌟🌟
🌟 每日一练刷题专栏 🌟
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
1. 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的。
如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。
每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。
示例 1:
输入:s = "egg", t = "add" 输出:true
示例 2:
输入:s = "foo", t = "bar" 输出:false
示例 3:
输入:s = "paper", t = "title" 输出:true
提示:
- 可以假设 s 和 t 长度相同。
出处:
https://edu.csdn.net/practice/25232849
代码:
import java.util.*;
public class isIsomorphic {
    public static class Solution {
        public boolean isIsomorphic(String s, String t) {
            if (s.length() != t.length()) {
                return false;
            }
            Map<Character, Character> somorphicMap = new HashMap<>();
            for (int i = 0; i < s.length(); i++) {
                char key = s.charAt(i);
                char value = t.charAt(i);
                if (somorphicMap.get(key) != null) {
                    if (somorphicMap.get(key) != value) {
                        return false;
                    }
                } else {
                    if (somorphicMap.containsValue(value)) {
                        return false;
                    }
                    somorphicMap.put(s.charAt(i), t.charAt(i));
                }
            }
            return true;
        }
    }
    public static void main(String[] args) {
        Solution sol = new Solution();
        String s = "egg", t = "add";
        System.out.println(sol.isIsomorphic(s, t));
        s = "foo"; t = "bar";
        System.out.println(sol.isIsomorphic(s, t));
        s = "paper"; t = "title";
        System.out.println(sol.isIsomorphic(s, t));
    }
}输出:
true
 false
 true
2. 随机字符串
生成一个由大写字母和数字组成的6位随机字符串,并且字符串不重复
出处:
https://edu.csdn.net/practice/25232848
代码:
public class generate {
    public static class Solution {
        public static char[] generate() {
        char[] letters = { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        boolean[] flags = new boolean[letters.length];
        char[] chs = new char[6];
        for (int i = 0; i < chs.length; i++) {
          int index;
          do {
            index = (int) (Math.random() * (letters.length));
          } while (flags[index]);
          chs[i] = letters[index];
          flags[index] = true;
        }
        return chs;
      }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        for (int i = 0; i < 10; i++)
            System.out.println(s.generate());
    }
}输出: (以下内容随机产生)
NARUCE
 YS6KCL
 JEUF3S
 F54XPV
 U1WJPH
 U1VFCZ
 1M92CN
 67IE5M
 VZ7QGM
 SR9IFE
3. 交错字符串
给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。
两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:
- s = s1 + s2 + ... + sn
- t = t1 + t2 + ... + tm
- |n - m| <= 1
- 交错 是 s1 + t1 + s2 + t2 + s3 + t3 + ...或者t1 + s1 + t2 + s2 + t3 + s3 + ...
提示:a + b 意味着字符串 a 和 b 连接。
示例 1:

输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" 输出:true
示例 2:
输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" 输出:false
示例 3:
输入:s1 = "", s2 = "", s3 = "" 输出:true
提示:
- 0 <= s1.length, s2.length <= 100
- 0 <= s3.length <= 200
- s1、- s2、和- s3都由小写英文字母组成
出处:
https://edu.csdn.net/practice/25240960
代码:
import java.util.*;
public class isInterleave {
    public static class Solution {
        public boolean isInterleave(String s1, String s2, String s3) {
            if ((s1.length() + s2.length()) != s3.length())
                return false;
            boolean[][] dp = new boolean[s2.length() + 1][s1.length() + 1];
            dp[0][0] = true;
            for (int i = 1; i <= s1.length(); i++) {
                dp[0][i] = dp[0][i - 1] && s1.charAt(i - 1) == s3.charAt(i - 1) ? true : false;
            }
            for (int i = 1; i <= s2.length(); i++) {
                dp[i][0] = dp[i - 1][0] && s2.charAt(i - 1) == s3.charAt(i - 1) ? true : false;
            }
            for (int i = 1; i < dp.length; i++) {
                for (int j = 1; j < dp[0].length; j++) {
                    dp[i][j] = (dp[i][j - 1] && s1.charAt(j - 1) == s3.charAt(i + j - 1))
                            || (dp[i - 1][j] && s2.charAt(i - 1) == s3.charAt(i + j - 1));
                }
            }
            return dp[s2.length()][s1.length()];
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        String s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac";
        System.out.println(s.isInterleave(s1, s2, s3));
        s1 = "aabcc"; s2 = "dbbca"; s3 = "aadbbbaccc";
        System.out.println(s.isInterleave(s1, s2, s3));
        s1 = ""; s2 = ""; s3 = "";
        System.out.println(s.isInterleave(s1, s2, s3));
    }
}输出:
true
 false
 true
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
|  | Golang每日一练 专栏 | 
|  | Python每日一练 专栏 | 
|  | C/C++每日一练 专栏 | 
|  | Java每日一练 专栏 | 


















