题意:如果两个字符串是对称的,就返回true。对称就是将串1中的同一字符都一起换掉,可以换成同串2一样的。

思路:ASCII码表哈希就行了。需要扫3次字符串,共3*n的计算量。复杂度O(n)。从串左开始扫,若字符没有出现过,则赋予其一个特定编号,在哈希表中记录,并将该字符改成编号。对串2同样处理。其实扫2次就行了,但是为了短码。

 class Solution {
public:
void cal(string &s)
{
int has[]={}, cnt=;
for(int i=; i<s.size(); i++)
{
if(!has[s[i]])
has[s[i]]=++cnt;
s[i]=has[s[i]];
}
} bool isIsomorphic(string s, string t) {
if(s.size()!=t.size()) return false;
if(s.size()==) return true;
cal(s);
cal(t);
for(int i=; i<s.size(); i++)
if(s[i]!=t[i]) return false;
return true;
}
};

AC代码

LeetCode Isomorphic Strings 对称字符串的更多相关文章

  1. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  2. [leetcode]205. Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  3. [LeetCode] Isomorphic Strings

    Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...

  4. [leetcode]205. Isomorphic Strings同构字符串

    哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...

  5. [LeetCode] Buddy Strings 伙计字符串

    Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...

  6. Python3解leetcode Isomorphic Strings

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

  7. 205 Isomorphic Strings 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...

  8. 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)

    题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...

  9. CodeForces985F:Isomorphic Strings (字符串&hash)

    题意:取出字符串Str里的两个串S,T,问对应位置的的字符在否有一一映射关系. hash:对于每个字符s=‘a’-‘z’,我们任意找一个i,满足Si==s,(代码里用lower_bound在区间找到最 ...

随机推荐

  1. C语言--递归问题

    1,一个经典的例子,理解递归过程的展开 #include<stdio.h> void func(int i){ ) func(i/); printf("%d",i) } ...

  2. java 随机颜色

    用HSV模型来实现颜色的随机,然后转为RGB模型 色相(H)是色彩的基本属性,就是平常所说的颜色名称,如红色.黄色等. 饱和度(S)是指色彩的纯度,越高色彩越纯,低则逐渐变灰,取0-100%的数值. ...

  3. 347. Top K Frequent Elements (sort map)

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  4. 3DMAX 7 角色建模1 人头建模

    说明: mesh与poly 可编辑多边形是一个多边形网格:即与可编辑网格不同,其使用超过三面的多边形.可编辑多边形非常有用,因为它们可以避免看不到边缘.例如,如果您对可编辑多边形执行切割和切片操作,程 ...

  5. 51nod1416(dfs)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1416 题意:中文题诶- 思路:dfs 搜索同一颜色的点.. 只 ...

  6. [Xcode 实际操作]九、实用进阶-(18)图像人脸识别:对图片中的人像进行面部检测

    目录:[Swift]Xcode实际操作 本文将演示对图片中的人像,进行面部检测. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //导入 ...

  7. hadoop 2.5.1单机安装部署伪集群

    环境:ubuntu 14.04 server 64版本 hadoop 2.5.1 jdk 1.6 部署的步骤主要参考了http://blog.csdn.net/greensurfer/article/ ...

  8. 学习Mahout(三)

    开发+运行第一个Mahout的程序 代码: /** * Licensed to the Apache Software Foundation (ASF) under one or more * con ...

  9. shell学习(6)- curl

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具. 语法 cur ...

  10. Codeforces Round #390 (Div. 2) B

    Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. ...