Leetcode 第205题 Isomorphic Strings

LoRa物联网实战:用新大陆模块打造智能LED呼吸灯(附完整代码解析) 本文详细介绍了如何利用新大陆LoRa模块实现工业级智能LED呼吸灯的设计与开发。通过256级PWM调制的呼吸灯效果,结合LoRa无线控制技术,实现设备状态的可视化反馈和多设备同步控制。文章包含完整的硬件选型、电路设计、算法优化和代码实现,特别适合物联网开发者和硬件工程师参考。 阅读详情

题目:Isomorphic Strings

  • Given two strings s and t, determine if they are isomorphic.

  • Two strings are isomorphic if the characters in s can be replaced to get t.

  • All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

    For example,
    Given “egg”, “add”, return true.

    Given “foo”, “bar”, return false.

    Given “paper”, “title”, return true.

  • Note:
    You may assume both s and t have the same length.


题目分析:

  • 这道题目的含义是判断两个字符串是否为同构即类似于abb和cdd或者abcc和cadd

思路:

  • 一开始自己理解的同构的思路是定义一个26长度的数组,存放字母出现的次数,然后将数组排序后对比就可以判断是否同构
  • 这个想法的缺陷是abb和bab的结果是同构的,但是事实是不是同构的

  • 更正思路:定义一个函数来将字符串更改为数字来替代例如abb用数字替换后为122;同理cdd用数字替换后也为122;那么即可判断两个字符串是同构的


代码:

  • Javascript:[错误代码]
/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isIsomorphic = function(s, t) {
    s=s.toUpperCase();
    t=t.toUpperCase();
    var arr1=new Array(26);
    var arr2=new Array(26);
    for(var i=0;i<26;i++)
    {
        arr1[i]=0;
        arr2[i]=0;
    }
    var arr3=s.split("");
    var arr4=t.split("");

    for(var i=0,l=arr3.length;i<l;i++)
    {
        var temp=arr3[i].charCodeAt()-65;//将字符对应的ASCII码的数组为加1
        arr1[temp]++;
    }
    for(var i=0,l=arr4.length;i<l;i++)
    {
        var temp=arr4[i].charCodeAt()-65;
        arr2[temp]++;
    }
    arr1=arr1.sort();//数组进行排序后如果两个数组相同,那么可以确定两个字符串是同构的
    arr2=arr2.sort();
    for(var i=0,l=arr1.length;i<l;i++)
    {
        if(arr1[i]!=arr2[i])
        {
            return false;
        }
    }
    return true;

};
错误:原因是因为算法的结果aba和aab是同构的  其实是不同构的  

  • Javascript:[正确]
function replaceStrToNum(s)
{
    var temp=0;
    //申请一个大小为128的数组,并且数组的值为0
    var arr= new Array(128);
    for(var i=0;i<128;i++)
    {
        arr[i]=0;
    }
    var strarr=s.split("");//将字符串s每一位放到数组strarr中
    for(var i=0,l=strarr.length;i<l;i++)
    {
        var a=strarr[i];
        if(arr[a.charCodeAt()]==0)
        {
            arr[a.charCodeAt()] +=temp+"";
            temp++;
        }
        strarr[i]=arr[a.charCodeAt()];
    }
    return strarr.join();//将数组连接成字符串
}
var isIsomorphic = function(s, t) {
    if(s.length!=t.length)
    {
        return false;
    }
    if(replaceStrToNum(s)!=replaceStrToNum(t))
    {
        return false;
    }
    return true;
}

  • C++:[正确]
class Solution {  
public:  
    bool isIsomorphic(string s, string t) {  
        if(s.length()!=t.length())//如果s和t的字符长度不同那么肯定不同构
        {
            return false;
        }
        if(replaceStrToNum(s)!=replaceStrToNum(t))//如果s和t的字符串被转换后不相等,那么肯定不同构
        {
            return false;
        }
        return true;
    } 
    string replaceStrToNum(string s){  
        char a[128]={0};//申请一个长度为128为的数组数组中值0
        char tmp='0';//单引号表示单个字符 双引号表示字符串
        for(int i=0,l=s.length();i<l;i++)
        {
            char b=s.at(i);//取出字符串s中index为i的单个字符
            if(a[b]==0)
            {
                a[b]=tmp++;
            }
            s[i]=a[b];
        }
        return s;
    }
}; 
系统安装(kuntaiR522 kvm安装) 鲲泰R522服务器kvm安装rocky 阅读详情

相关推荐

PyCharm中安装OpenCV全指南:从环境配置到报错解决

Python开发中,环境配置往往是新手入门的第一道坎。理解解释器、虚拟环境与包管理工具pip之间的关系,是解决ModuleNotFoundError等问题的关键。PyCharm作为常用IDE,其项目默认创建独立虚拟环境,若在系统终端安装的OpenCV与该环境不一致,便会导致import cv2失败。OpenCV作为计算机视觉领域的基础库,广泛应用于图像处理、摄像头调用及深度学习预处理等场景。掌握基于虚拟环境的安装与验证方法,不仅能一次性解决cv2导入错误,还能为后续项目隔离依赖、避免版本冲突打下基础。本文从

weixin_31742871的博客 303

JZ54---字符流中第一个不重复的字符

题目 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。 输出描述 如果当前字符流没有存在出现一次的字符,返回#字符。 思路 使用LinkedHashMap 链接输入的所有值。它的优点在于,不会自动排序,你是好久进入的map集合,它就在最后面追加上。如果出现以前重复的值,修改的位置就在以前那个位置 遍历map集合,找到第一个值为1的那个字

Eva_xiaoxi的博客 947

射频电源与等离子体技术:原理、应用与优化策略

射频电源(RF Power Supply)作为高频交流电信号发生器,在半导体制造和材料处理中扮演关键角色。其核心原理是通过高频电场激发气体产生等离子体,这种电离气体包含电子、离子和活性基团,具有独特的非平衡特性。技术价值体现在等离子体密度和电子温度的可控性上,使得在刻蚀和沉积工艺中能精确调控材料处理效果。应用场景广泛覆盖PECVD、深槽刻蚀等工业流程,其中13.56MHz ISM频段因其全球合规性成为行业标准。优化策略包括双频驱动和阻抗匹配网络设计,可显著提升工艺均匀性和设备效率。

weixin_29163797的博客 405

[题解&总结]LeetCode第205场周赛

LeetCode第205场周赛 比赛地址 A:替换所有的问号 题意 给定一个只含小写字母和?的字符串,现需要将所有的问号改为小写字母,切不能和与其相连的字符相同。 题解 直接暴力即可。遍历整个字符串,出现?号判断即可。 AC代码(cpp) class Solution { public: string modifyString(string s) { int len=s.length(); for(int i=0;i<len;i++){

serendipity的博客 470

告别Modern Standby噩梦:Win11睡眠断网冻结的根源剖析与S3睡眠回归实战

本文深入剖析了Windows 11中Modern Standby(现代待机)模式导致睡眠后断网、系统冻结等问题的根源,指出其与硬件驱动、后台活动的兼容性矛盾。文章提供了通过修改注册表键值强制系统回归稳定S3睡眠状态的详细实战步骤,帮助用户彻底解决睡眠故障,实现可靠的远程访问与挂机任务。

weixin_29210727的博客 628

别再手动对照了!用Python+ArcGIS Pro一键搞定三调数据转土地三大类

本文介绍如何利用Python和ArcGIS Pro实现三调数据向土地三大类的自动化转换,大幅提升国土空间规划和土地变更调查的工作效率。通过封装分类规则为Python函数并集成到ArcGIS Pro脚本工具中,解决了传统手动操作耗时且易错的问题,适用于大规模数据处理场景。

weixin_29081101的博客 316

字符串同构问题——leetcode205/leetcode290

205. Isomorphic Strings 问题描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a cha

白夜行 1962

RAGFlow 离线部署:解决cl100k_base.tiktoken问题

摘要 本文详细解决了RAGFlow离线部署时tiktoken缓存文件下载失败的问题。通过分析tiktoken库的缓存机制,一次性不改代码解决.

baidu_25691461的博客 843

LeetCode第 205 题:同构字符串(C++)

205. 同构字符串 - 力扣(LeetCode) 其实就是一一映射问题 哈希表映 初始代码: class Solution { public: bool isIsomorphic(string s, string t) { unordered_map<char, char> m; unordered_set<char> set; for(int i = 0; i < s.size(); ++i){

zj 419

深入解析LinuxCNC源码:从界面开发到硬件交互

本文深入解析LinuxCNC开源数控系统的源码架构与二次开发实践。详细介绍了从GTK+界面定制、Python脚本绑定,到硬件抽象层(HAL)配置与核心运动控制模块的完整开发流程。通过实战案例,帮助开发者掌握如何基于LinuxCNC源码进行界面开发和硬件交互定制,实现个性化数控系统。

weixin_29164843的博客 555

第77步 时间序列建模实战:多因素预测 vol-2(以决策树回归为例)

第77步 时间序列建模实战:多因素预测 vol-2(以决策树回归为例)

Jet4505的博客 1014

205. Isomorphic Strings

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with anot

qq_32176879的博客 224

部署OpenClaw时设置环境变量提示export: not valid in this context的完整解决方案

OpenClaw环境变量设置错误解决方案 问题概述 在部署OpenClaw时设置环境变量出现"export: not valid in this context"错误,通常由以下原因导致: 在非交互式shell中使用export命令 在命令替换或管道中使用export 脚本文件包含Windows换行符(CRLF) 解决方案 修正脚本执行方式:确保export命令在正确的shell上下文中使用 使用source命令:通过. setup_openclaw.sh或source setup_op

博客 465

Leetcode 205题:Isomorphic Strings

题目描述 给定两个字符串,检查它们是否是结构相似的。 对于结构相似的定义如下 Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters m

weixin_44607838的博客 300

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

leetcode python3 简单题205. Isomorphic Strings

1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零五题 (1)题目 英文: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be repla

从此做一个俗人 240

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Leetcode 刷题 - 205 - Isomorphic Strings

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with anot

257

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【Leetcode刷题笔记】205. Isomorphic Strings

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another ch...

知吾朱期应庆的博客 143

leetCode刷题记录32_205_Isomorphic Strings

/*****************************************************问题描述************************************************* Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if t...

gunsmoke的专栏 144

【LeetCode 面试经典150题】205. Isomorphic Strings 同构字符串

Given two strings and , determine if they are isomorphic.Two strings and are isomorphic if the characters in can be replaced to get .All occurrences of a character must be replaced with another character while preserving the order of characters. No two

qq_43513394的博客 489

leetcode刷题日记:205. Isomorphic Strings(同构字符串)

根据字符的两种表现形式我们可以建立数组描述映射。我们可以发现在遍历字符串s与t,将对应位置的元素映射关系逐一加入到f数组中,在加入的过程中如果发现映射已存在,但是映射的元素不相同,说明这两个不是同构字符串,我们返回false,一直到结束,如果s中所有元素都与t中对应位置的元素建立了映射,则我们需要检查是否存在多个s中的不同字符映射到了t中的同一个字符上,如果存在返回false,否则返回true。至此程序结束。i < 128;

apprentice_eye的博客 215

LeetCode 刷题--205 Isomorphic Strings同构字符串

205 Isomorphic StringsGiven two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be repl...

shhdxxqhbeth的博客 192

leetcode刷题笔记(Golang)--205. Isomorphic Strings

Isomorphic Strings iven two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be repl...

weixin_44555304的博客 171

[LeetCode]205. Isomorphic Strings 解题报告(C++)

[LeetCode]205. Isomorphic Strings 解题报告(C++) 题目描述 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occu...

【黑键】 577

leetcode:205 Isomorphic Strings-每日编程第三十八题

Isomorphic Strings Total Accepted: 39963 Total Submissions: 145129 Difficulty: Easy Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the charac

易水的博客 354

500. Keyboard Row\205. Isomorphic Strings\453. Minimum Moves to Equal Array Elements

Keyboard Row 题目描述 代码实现500. Keyboard Row题目描述Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.把一个字符串中的字符来源于键

I AM BACK 558

【leetcode刷题】[简单]205. 同构字符串(isomorphic strings)-java

同构字符串 isomorphic strings题目分析解答 题目 给定两个字符串 s 和 t,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。 示例 1: 输入: s = "egg", t = "add" 输出: true 示例 2: 输...

mikuluna的博客 355
上一篇: Leetcode 第228题 Summary Ranges
下一篇: Leetcode 第191题 Number of 1 Bits
newbie_ljm
博客等级 码龄11年 1粉丝 29原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值