12
返回列表 发新帖
楼主: butzhang
跳转到指定楼层
上一主题 下一主题
收起左侧

一道谷歌面经 “字符串转换”/transision 个人总结

🔗
thuxx 2019-10-5 11:00:50 | 只看该作者
全局:
  1. import java.util.*;

  2. // use any letter (26) to transfrom String a to String b, one time change all one type of letters
  3. // aacc => bbdd, aacc => bbcc => bbdd
  4. // abc => bca, must get the help of letters which are not in the input string, abc => bbc => ccc?, abc => ebc => eba => eca => bca
  5. // thus if all 26 distinct chars are in the 2 input string respectively, no possible ways only if abc...z => abc..z (no need to change)
  6. class Solution {
  7.     public boolean solution(String a, String b) {
  8.         if (a == null || b == null) {
  9.             return false;
  10.         }
  11.         if (a.length() != b.length()) {
  12.             return false;
  13.         }

  14.         Map<Character, Character> map = new HashMap<>();
  15.         for (int i = 0; i < a.length(); i++) {
  16.             Character ca = a.charAt(i);
  17.             Character cb = b.charAt(i);
  18.             map.putIfAbsent(ca, cb);
  19.             if (map.get(ca) != cb) {
  20.                 return false;
  21.             }
  22.         }

  23.         // find cycle, if map.keySet() and map.values() are all 26 distinct characters, and exist map.get(i) != i, which means cycle exist
  24.         int diff = 0;
  25.         Set<Character> set = new HashSet<>();
  26.         for (Character c : map.keySet()) {
  27.             set.add(map.get(c));
  28.             // if values() contains 26 distinct characters which means keySet() must contain 26 distinct characters
  29.             // abcd...z => abcd..z, abcd...z => bbcd...z(only 25), abcd...z => bacd..z
  30.             // has no redundant letter to do the 'bridge' transition
  31.             if (map.get(c) != c) {
  32.                 diff++;
  33.             }
  34.         }

  35.         if (diff > 0 && set.size() == 26) {
  36.             return false;
  37.         }

  38.         return true;
  39.     }

  40.     public static void canTransform(String a, String b, boolean result) {
  41.         Solution s = new Solution();
  42.         System.out.println(s.solution(a, b) == result);
  43.     }

  44.     public static void main(String[] args) {
  45.         canTransform("abcdefghijklmnopqrstuvwxyz","abcdefghijklmnopqrstuvwxya", true); // z => a, true
  46.         canTransform("abcdefghijklmnopqrstuvwxya","abcdefghijklmnopqrstuvwxyz", false); // a => a / z, false
  47.         canTransform("aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz","bbaaccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz", false);// false
  48.         canTransform("abba","baab", true); // true
  49.         canTransform("abaca", "ebece", true); // true
  50.         canTransform("abba","bbbb", true); // true
  51.         canTransform("abc","cba", true); // true
  52.         canTransform("aac","abc", false); // false
  53.         canTransform("abc", "bca", true); // true
  54.     }
  55. }
复制代码
回复

使用道具 举报

🔗
thuxx 2019-10-5 11:19:02 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表