• 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 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.

    链接:  http://leetcode.com/problems/isomorphic-strings/

    2/24/2017

    需要用2个hashmap,aa - > ab是不成立的

     1 public class Solution {
     2     public boolean isIsomorphic(String s, String t) {
     3         if (s.length() != t.length()) return false;
     4 
     5         HashMap<Character, Character> h1 = new HashMap<Character, Character>();
     6         HashMap<Character, Character> h2 = new HashMap<Character, Character>();
     7         char a, b;
     8 
     9         for (int i = 0; i < s.length(); i++) {
    10             a = s.charAt(i);
    11             b = t.charAt(i);
    12             if (h1.containsKey(a)) {
    13                 if (h1.get(a) != b) return false;
    14                 h1.put(a, b);
    15             } else {
    16                 h1.put(a, b);
    17             }
    18             if (h2.containsKey(b)) {
    19                 if (h2.get(b) != a) return false;
    20             } else {
    21                 h2.put(b, a);
    22             }
    23         }
    24         return true;
    25 
    26     }
    27 }

    别人的好算法可以用bitmap,留给二刷。

  • 相关阅读:
    mysql查询版本
    mysql导出bug备注
    linux查找文件
    linux 编译式安装nginx
    linux文件校验
    linux 编译式安装apache
    网络层
    数据链路层
    物理层
    servlet过滤器
  • 原文地址:https://www.cnblogs.com/panini/p/6440840.html
Copyright © 2020-2023  润新知