📣 独立日限时特惠: VIP通行证立减$68
123
返回列表 发新帖
楼主: mantishrimp
跳转到指定楼层
上一主题 下一主题
收起左侧

onsite

🔗
say543 2016-12-5 08:34:57 | 只看该作者
全局:
treeguard 发表于 2016-12-4 15:58
可以hash 啊 对每一个单词 比如egg, e->1 g->2

这样会把不同的character hash 成一样的value? 目的是?
回复

使用道具 举报

🔗
swissashley 2017-4-29 13:11:55 | 只看该作者
全局:
say543 发表于 2016-12-5 08:34
这样会把不同的character hash 成一样的value? 目的是?

回覆一下follow up, 有問題請指正:
Javascript:
// Given a string array, group the isomorphic strings into groups.
function isomorphicGroup(arr) {
  if (arr === null || arr.length === 0) return [];
  let result = [];

  // Use a helper method to build unique hash for different patterns
  function encoding(str) {
    let i = 0;
    let hash = "";
    let map = {};
    for(let c of str) {
      if (map[c] === undefined) {
        map[c] = i++;
        hash += map[c];
      }
    }
    return hash;
  }
  let isoMap = {};
  for (let str of arr) {
    // Unique pattern
    let hashStr = encoding(str);

    // Group the same pattern strings into same array.
    if (isoMap[hashStr] === undefined) {
      isoMap[hashStr] = [str];
    } else {
      isoMap[hashStr].push(str);
    }
  }
  // Build the result array
  for (let key in isoMap) {
    result.push(isoMap[key]);
  }
  return result;
}

let strArr = ['aba', 'zzz','dccb', 'ded', 'ccc', 'aaa', 'cbba'];
console.log(isomorphicGroup(strArr));
// [ [ 'zzz', 'ccc', 'aaa' ], [ 'aba', 'ded' ], [ 'dccb', 'cbba' ] ]
回复

使用道具 举报

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

本版积分规则

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