Jan 10 - Isomorphic Strings; String; Data Type: char; digit and letter

创建两个数组 分别记录两个字符串 各个位置字符出现的上一个位置 通过比较当前字符的上一个位置是否相同 判断是否同构

比较坑爹的是 一开始以为只有字母 所以纠结于怎么找出字母来。。。

代码如下:

public class IsomorphicStrings {

public static boolean isIsomorphic(String s, String t) {

int[] alpha1 = new int[1000];

int[] alpha2 = new int[1000];

for(int i = 0; i < s.length(); i++){

int from_a1 = (int) (s.charAt(i) - ‘a‘);

int from_A1 = (int) (s.charAt(i) - ‘A‘);

int from_a2 = (int) (t.charAt(i) - ‘a‘);

int from_A2 = (int) (t.charAt(i) - ‘A‘);

if(((from_a1 < 0 || from_a1 > 25) && (from_A1 < 0 || from_A1 > 25)) &&

((from_a2 < 0 || from_a2 > 25) && (from_A2 < 0 || from_A2 > 25))){

System.out.print(from_a1 + " ");

System.out.print(from_A1 + " ");

System.out.print(from_a2 + " ");

System.out.print(from_A2 + " ");

System.out.println();

}

else if( ((from_a1 >= 0 && from_a1 <= 25) || (from_A1 >= 0 && from_A1 <= 25)) &&

((from_a2 >= 0 && from_a2 <= 25) || (from_A2 >= 0 && from_A2 <= 25))){

System.out.print(alpha1[from_a1] + " ");

System.out.print(from_a1 + " ");

System.out.print(alpha2[from_a2] + " ");

System.out.print(from_a2 + " ");

System.out.println();

if(alpha1[from_a1] != alpha2[from_a2]) return false;

alpha1[from_a1] = i+1;

alpha2[from_a2] = i+1;

//continue;

}

else return false;

}

return true;

}

无视之

更新后:

public class Solution {
public boolean isIsomorphic(String s, String t) {
int[] alpha1 = new int[128];
int[] alpha2 = new int[128];

for(int i = 0; i < s.length(); i++){
int from_a1 = (int) (s.charAt(i));
int from_a2 = (int) (t.charAt(i));
if(alpha1[from_a1] != alpha2[from_a2]) return false;
alpha1[from_a1] = i + 1;
alpha2[from_a2] = i + 1;
}
return true;
}
}

时间: 2024-10-12 14:34:45

Jan 10 - Isomorphic Strings; String; Data Type: char; digit and letter的相关文章

Jan 10 - Reverse Linked List;Data Structure; Linked List; Pointer; Iteration &amp; Recursion

Iteration: 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode reverseList(ListNode head) { if(head == null) return null; List

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

翻译 给定两个字符串s和t,决定它们是否是同构的. 如果s中的元素被替换可以得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换另一个字符串的元素的过程中,所有字符的顺序必须保留. 没有两个字符可以被映射到相同的字符,但字符可以映射到该字符本身. 例如, 给定"egg","add",返回真. 给定"foo","bar",返回假. 给定"paper","title",返回真. 批

LeetCode之“散列表”: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 c

7.1The ISO SQL Data Type

In this section we introduce the data types defined in the SQL standard. We start by defining what constitutes a vailed identifer in SQL. 7.1.1  SQL Identifiers SQL identifiers are identify objects in the database, such as table names, views names, a

leetcode 204/187/205 Count Primes/Repeated DNA Sequences/Isomorphic Strings

一:leetcode 204 Count Primes 题目: Description: Count the number of prime numbers less than a non-negative number, n 分析:此题的算法源码可以参看这里,http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 代码: class Solution { public: int countPrimes(int n) { // 求小于一个数n的素数个

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: 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 occurre

PHP 笔记一(systax/variables/echo/print/Data Type)

PHP stands for "Hypertext Preprocessor" ,it is a server scripting language. What Can PHP Do? PHP can generate dynamic page content PHP can create, open, read, write, delete, and close files on the server PHP can collect form data PHP can send an

Linux C double linked for any data type

/************************************************************************** * Linux C double linked for any data type * 声明: * 提供一种双链接口,可以保存保存任何类型的数据. * * 2015-12-25 晴 深圳 南山平山村 曾剑锋 **********************************************************************

Isomorphic Strings leetcode

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.