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

  • 相关阅读:
    手搓一个兔子问题(分享一个C语言问题,持续更新...)
    一个C语言萌新的学习之旅(持续更新中...)
    ...续上文(一个小萌新的C语言之旅)
    手搓一个C语言简单计算器。
    嘿,C语言(持续更新中...)
    一个博客萌新的博客之旅。。。。
    vue+uikit3+laravel快速建站
    mpvue开发博客园小程序
    C语言俄罗斯方块小游戏练习
    c语言贪吃蛇小游戏练习
  • 原文地址:https://www.cnblogs.com/apanda009/p/7797164.html
Copyright © 2011-2022 走看看