第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

找出字符串是通用字符串嗎?

找出字符串是通用字符串嗎?

qq_笑_17 2022-05-14 14:37:59
這是在一次采訪中問(wèn)我的問(wèn)題,通過(guò)制作一個(gè)函數(shù)來(lái)找出通用字符串,如果有更好的解決方案,他讓我用 ES6 來(lái)做,那么請(qǐng)幫助我以更好和更短的方式解決它。如果字符串是通用字符串,此函數(shù)將返回 true,否則將返回 falseconst universalString = (string) => {  string = string.replace(/ /g, '').toLowerCase();  let cherecterLenData = [];  // array of alphabets  let alphabetData = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];  // filter with array of alphabets  alphabetData.filter((charecter, charecterIndex) => {    if (string.indexOf(charecter) != -1) {      `check if the character present in a -given string`      // No. of times a character present in a string       let stringCherecterLen = string.split(charecter).length - 1;      if (stringCherecterLen != 0) {        cherecterLenData.push(stringCherecterLen);      }    }  });  if (cherecterLenData.every((val, i, arr) => val === arr[0])) {    return true;  }  return false;}console.log(universalString("Hello Rajat Srivastava How Are You"));console.log(universalString("aabbcchh llkkuudd"));console.log(universalString("abbabcbdbabdbdbabababcbcbab")); // falseconsole.log(universalString("aaccbbddffee")); // true
查看完整描述

1 回答

?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

聽(tīng)起來(lái)像“通用字符串”,其中每個(gè)字母字符出現(xiàn)的次數(shù)與字符串中每個(gè)其他字母字符的出現(xiàn)次數(shù)相同。


解決此問(wèn)題的一種方法是制作頻率計(jì)數(shù)器,然后檢查所有值是否相同:


const frequencyCount = a => a.reduce((a, e) => {

  a[e] = ++a[e] || 1;

  return a;

}, {});


const universalString = s => {

  const alpha = [...s.replace(/[^a-z]/gi, "")];

  const counts = Object.values(frequencyCount(alpha));

  return !counts.length || counts.every(e => e === counts[0]);

};


[

  "aabbcchh llkkuudd",

  "abbabcbdbabdbdbabababcbcbab",

  "aaccbbddffee"

].forEach(e => console.log(e, "=>", universalString(e)));


查看完整回答
反對(duì) 回復(fù) 2022-05-14
  • 1 回答
  • 0 關(guān)注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)