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 的进行转译看是否相同来进行添加 

  • 相关阅读:
    HDU_5372 树状数组 (2015多校第7场1004)
    《 字典树模板_递归 》
    《神、上帝以及老天爷》
    《Crazy tea party》
    UVA_ Overflow
    UVA_If We Were a Child Again
    UVA_Product
    UVA_Integer Inquiry
    你也可以屌到爆的这样敲代码当黑客!
    大数相加_原创
  • 原文地址:https://www.cnblogs.com/apanda009/p/7797164.html
Copyright © 2011-2022 走看看