String 同构字符串
给定两个字符串s和t,确定它们是否同构 如果s中的字符可以替换为t,则两个字符串是同构的 在保留字符顺序的同时,必须用另一个字符替换出现的所有字符。两个字符不能映射到同一个字符,但一个字符可以映射到自身String 同构字符串,string,data-structures,hashmap,set,String,Data Structures,Hashmap,Set,给定两个字符串s和t,确定它们是否同构 如果s中的字符可以替换为t,则两个字符串是同构的 在保留字符顺序的同时,必须用另一个字符替换出现的所有字符。两个字符不能映射到同一个字符,但一个字符可以映射到自身 public bool isomorphic(string str1, string str2) { if (str1.Length != str2.Length) { return false;
public bool isomorphic(string str1, string str2)
{
if (str1.Length != str2.Length)
{
return false;
}
var str1Dictionary = new Dictionary<char, char>();
var str2Dictionary = new Dictionary<char, char>();
var length = str1.Length;
for (int i = 0; i < length; i++)
{
if (str1Dictionary.ContainsKey(str1[i]))
{
if (str1Dictionary[str1[i]] != str2[i])
{
return false;
}
}
else
{
str1Dictionary.Add(str1[i], str2[i]);
}
if (str2Dictionary.ContainsKey(str2[i]))
{
if (str2Dictionary[str2[i]] != str1[i])
{
return false;
}
}
else
{
str2Dictionary.Add(str2[i], str1[i]);
}
}
return true;
}
比如说,,
给定“egg”,“add”,返回true
给定“foo”、“bar”,返回false
给定“论文”、“标题”,返回true
注:
你可以假设s和t的长度相同
我有这个解决方案,但它花费了太多的时间。
任何好的解决方案都将不胜感激
public boolean isIsomorphic(String s, String t) {
String resString1="",resString2="";
HashMap<Character,Integer> hashmapS = new HashMap();
HashMap<Character,Integer> hashmapT = new HashMap();
boolean flag = false;
for(int i = 0;i<s.length();i++)
{
char chS = s.charAt(i);
char chT = t.charAt(i);
if(hashmapS.containsKey(chS))
{
resString1 = resString1 + hashmapS.get(chS);
}
else
{
resString1 = resString1 + i;
hashmapS.put(chS, i);
}
if(hashmapT.containsKey(chT))
{
resString2 = resString2 + hashmapT.get(chT);
}
else
{
resString2 = resString2 + i;
hashmapT.put(chT, i);
}
}
if(resString1.equals(resString2))
return true;
else
return false;
}
公共布尔同态(字符串s,字符串t){
字符串resString1=“”,resString2=“”;
HashMap hashmapS=新的HashMap();
HashMap hashmapT=新的HashMap();
布尔标志=假;
对于(int i=0;i
不过,你应该自己弄清楚算法。如果可以重新映射单个单词中的字母以获得第二个单词,则两个单词被称为同构。重新映射一个字母意味着在字母请求保持不变的情况下,用另一个字母替换它的所有事件。没有两个字母可以指向同一个字母,但是这封信可以自己写
public bool isomorphic(string str1, string str2)
{
if (str1.Length != str2.Length)
{
return false;
}
var str1Dictionary = new Dictionary<char, char>();
var str2Dictionary = new Dictionary<char, char>();
var length = str1.Length;
for (int i = 0; i < length; i++)
{
if (str1Dictionary.ContainsKey(str1[i]))
{
if (str1Dictionary[str1[i]] != str2[i])
{
return false;
}
}
else
{
str1Dictionary.Add(str1[i], str2[i]);
}
if (str2Dictionary.ContainsKey(str2[i]))
{
if (str2Dictionary[str2[i]] != str1[i])
{
return false;
}
}
else
{
str2Dictionary.Add(str2[i], str1[i]);
}
}
return true;
}
public bool同构(字符串str1、字符串str2)
{
if(str1.Length!=str2.Length)
{
返回false;
}
var str1Dictionary=新字典();
var str2Dictionary=newdictionary();
变量长度=str1.长度;
for(int i=0;i
在您的实现中,只有在完全处理两个字符串之后,您才会知道答案。而在许多否定测试案例中,可以看到第一个冲突本身来确定答案
例如,考虑1000个字符长字符串:“AA..…”和“BA……”。一个优雅的解决方案必须返回看到两个字符串的第二个字符本身,因为“A”不能在这里映射到“A”和“B”。
<>你可能会发现有用的东西。它也指向一个基于C++的解决方案。
需要注意的重要事项是:
- 由于可能的元素数将是max pow(2,sizeof(char)),因此保留您自己的哈希值(ASCII代码本身就是密钥)是很有帮助的。它比使用通用哈希表有显著的改进
在C++中,使用STD::URReordEdmap比STD::MAP和STD::STL更好,因为后者只使用平衡二叉搜索树。
这里是另一个实现,但内存使用较少
公共类同构{
私有静态布尔isIsomorphic(字符串s、字符串t){
如果(s.length()!=t.length()){
返回false;
}
字符1[]=新字符[26];
字符2[]=新字符[26];
字符数组1[]=s.toCharArray();
char array2[]=t.toCharArray();
对于(inti=0;i/*时间复杂度=O(n)*/
public静态布尔isIsomorphic(字符串s1,字符串s2){
如果(s1==null | | s2==null){
抛出新的IllegalArgumentException();
}
如果(s1.length()!=s2.length()){
返回false;
}
HashMap=newHashMap();
对于(int i=0;i
公共类同构{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
系统输出打印LN(isIsomorphic(“foo”,“bar”));
系统输出打印LN(异态(“bar”、“foo”);
系统输出打印LN(isIsomorphic(“foo”,“bar”));
系统输出打印LN(异态(“bar”、“foo”);
System.out.println(异形体(“海龟”、“海龟”);
System.out.println(雌雄同体(“海龟”);
System.out.println(异形体(“海龟”、“海龟”);
System.out.println(雌雄同体(“海龟”);
}
公共静态布尔isIsomorphic(字符串s1、字符串s2){
如果(s1.length()!=s2.length()){
返回false;
}
如果(s1.length()==1){
返回true;
}
int c1;
int c2;
对于(inti=0;i这里是我的实现
private static boolean isIsmorphic(String string1, String string2) {
if(string1==null) return false;
if(string2==null) return false;
if(string1.length()!=string2.length())return false;
HashMap<Character,Character> map=new HashMap<>();
for(int i=0;i<string1.length();i++){
char c1=string1.charAt(i);
char c2=string2.charAt(i);
if(map.get(c1)!=null && !map.get(c1).equals(c2)){
return false;
}
map.put(c1, c2);
}
return true;
}
private静态布尔Isismophic(字符串string1,字符串string2){
if(string1==null)返回false;
if(string2==null)返回false;
如果(string1.length()!=string2.length())返回false;
HashMap=newHashMap();
对于(int i=0;i公共类解决方案{
公共布尔同态(字符串s,字符串t){
int[]m=新int[512];
对于(int i=0;iprivate static boolean isIsmorphic(String string1, String string2) {
if(string1==null) return false;
if(string2==null) return false;
if(string1.length()!=string2.length())return false;
HashMap<Character,Character> map=new HashMap<>();
for(int i=0;i<string1.length();i++){
char c1=string1.charAt(i);
char c2=string2.charAt(i);
if(map.get(c1)!=null && !map.get(c1).equals(c2)){
return false;
}
map.put(c1, c2);
}
return true;
}
public class Solution {
public boolean isIsomorphic(String s, String t) {
int[] m = new int[512];
for (int i = 0; i < s.length(); i++) {
if (m[s.charAt(i)] != m[t.charAt(i)+256]) return false;
m[s.charAt(i)] = m[t.charAt(i)+256] = i+1;
}
return true;
}
}
public bool IsIsomorphic(string s, string t)
{
if (s == null || s.Length <= 1) return true;
Dictionary<char, char> map = new Dictionary<char, char>();
for (int i = 0; i < s.Length; i++)
{
char a = s[i];
char b = t[i];
if (map.ContainsKey(a))
{
if (map[a]==b)
continue;
else
return false;
}
else
{
if (!map.ContainsValue(b))
map.Add(a, b);
else return false;
}
}
return true;
}
static boolean isIsomorphic(String s1, String s2) {
if (s1 == null || s2 == null) return false;
final int n = s1.length();
if (n != s2.length()) return false;
for (int i = 0; i < n; i++) {
final char c1 = s1.charAt(i);
final char c2 = s2.charAt(i);
for (int j = i + 1; j < n; j++) {
if (s1.charAt(j) == c1 && s2.charAt(j) != c2) return false;
if (s2.charAt(j) == c2 && s1.charAt(j) != c1) return false;
}
}
return true;
}
boolean isIsomorphic(String s, String t){
HashMap<Character, Character> map = new HashMap<>();
HashSet<Character> set = new HashSet<>();
if(s.length() != t.length())
return false;
for (int i = 0; i < s.length(); i++) {
if(map.containsKey(s.charAt(i))){
if(map.get(s.charAt(i)) != t.charAt(i))
return false;
} else if(set.contains(t.charAt(i))) {
return false;
} else {
map.put(s.charAt(i), t.charAt(i));
set.add(t.charAt(i));
}
}
return true;
}
public boolean areIsomorphic(String s1,String s2)
{
if(s1.length()!=s2.length())
return false;
int count1[] = new int[256];
int count2[] = new int[256];
for(int i=0;i<s1.length();i++)
{
if(count1[s1.charAt(i)]!=count2[s2.charAt(i)])
return false;
else
{
count1[s1.charAt(i)]++;
count2[s2.charAt(i)]++;
}
}
return true;
}
public bool isIsomorphic(String string1, String string2)
{
if (string1 == null || string2 == null)
return false;
if (string1.Length != string2.Length)
return false;
var data = new Dictionary<char, char>();
for (int i = 0; i < string1.Length; i++)
{
if (!data.ContainsKey(string1[i]))
{
if (data.ContainsValue(string2[i]))
return false;
else
data.Add(string1[i], string2[i]);
}
else
{
if (data[string1[i]] != string2[i])
return false;
}
}
return true;
}