zoukankan      html  css  js  c++  java
  • Isomorphic Strings + iso follow up

    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.

    这里用一个哈希表,但是用map.values().contains() 来查看是否一一映射, 因为要排除egg->ddd这种多对一的映射。

    先问:是否正确?

    given "ab", "ca"; returns true 
    we can map 'a' -> 'c', 'b'

    Use HashMap, but HashMap does not guarantee values() set to be unique. Here we require no two characters may map to the same character. So we should also check if map.values() have duplicates. If yes, return false.

    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            if (s==null || t==null || s.length() != t.length()) return false;
            HashMap<Character, Character> map = new HashMap<Character, Character>();
            for (int i=0; i<s.length(); i++) {
                if (!map.containsKey(s.charAt(i))) {
                    if (!(map.values().contains(t.charAt(i)))) {
                        map.put(s.charAt(i), t.charAt(i));
                    }
                    else return false;
                }
                else if (map.get(s.charAt(i)) != t.charAt(i)) return false;
            }
            return true;
        }
    }

     

    俩hashmap:

    public static boolean check(String s,String t){
    		if(s.length()!=t.length()) return false;
    		HashMap<Character,Character> map1=new HashMap<Character, Character>();
    		HashMap<Character,Character> map2=new HashMap<Character, Character>();
    		
    		for(int i=0;i<s.length();i++){
    			char c1=s.charAt(i);
    			char c2=t.charAt(i);
    			if(map1.containsKey(c1)){
    				if(map1.get(c1)!=c2) return false;
    			}
    			if(map2.containsKey(c2)){
    				if(map2.get(c2)!=c1) return false;
    			}
    			
    			map1.put(c1, c2);
    			map2.put(c2, c1);
    		}
    		return true;
    	}
    

      

    205. Follow up: 如果是三个String 怎么做同下

     

    /*
    205的follow up,给一个string数组,
    将iso的词归在一组 {'fff','abc','foo','haa','www','vvv'}-> { {'fff',www','vvv'} , {'haa','foo'} , {'abc'} }
     */
    public class Test {
        //boolean a = false;
        public void sqrt(String[] words) {
            List<List<Integer>> ans = new ArrayList<>();
            if (words == null || words.length == 0) {
                System.out.println(false);
                return;
            }
            Map<String, HashSet<String>> map = new HashMap<>();
            for (String s : words) {
                char beg = 'a';
                HashMap<Character, Character> dic = new HashMap<>();
                StringBuilder sb = new StringBuilder();
                for (char c : s.toCharArray()) {
                    if (!dic.containsKey(c)) {
                        dic.put(c, beg);
                        beg++;
                    }
                    sb.append(dic.get(c));
                }
                if (!map.containsKey(sb.toString())) {
                    map.put(sb.toString(), new HashSet<String>());
    
                }
                map.get(sb.toString()).add(s);
    
            }
            for (HashSet<String> sub : map.values()) {
                for (String i : sub) {
                    System.out.print(i);
                    System.out.print(" ");
                }
                System.out.println();
            }
    
    
        }
    
        public static void main(String[] args) {
            Test t = new Test();
            int[] nums = new int[]{2,1,4,5,6};
            String[] words = new String[]{"fff","abc","foo","haa","www","vvv"};
            t.sqrt(words);
        }
    }
    

     number cipher match pairs, 类似LC那道题,输入是一个string和一组string,如下:
    abb, {"bcc", "abc", "cdd"}
    输出是{"bcc", "cdd"}

    方法同上,先把第一个string 转译,建hashmap<string, set> 再对array 的进行转译看是否相同来进行添加 

  • 相关阅读:
    Vmware15的安装以及Ubunt的在虚拟机上的安装
    MYSQL数据库查询索引
    SpringBoot和mybatis整合报错:Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 152; columnNumber: 10; 元素类型为 "mapper"
    idea中导包出现import org.apach.*,提交代码老出现冲突,不想使用.*的设置
    idea打断点后发现被标记的断点处那一行整行被标记了其他颜色,前面没有断点标识的红点
    Intellj IDEA 光标显示insert状态解决办法
    AngularJS 单元测试 Karma jasmine
    npm 及安装
    AngularJS 承诺 Promise
    AngularJS 路由及SPA理解
  • 原文地址:https://www.cnblogs.com/apanda009/p/7797164.html
Copyright © 2011-2022 走看看