1. 程式人生 > >codewars解題筆記 —— 字串匹配

codewars解題筆記 —— 字串匹配

題目

In this kata you are required to, given a string, replace every letter with its position in the alphabet.

If anything in the text isn't a letter, ignore it and don't return it
a being 1, b being 2, etc.
As an example:
alphabet_position("The sunset sets at twelve o' clock.")

Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" as a string.

將傳入的字串,轉化為每個字母對應的順序組成數字字串。

我的答案

function alphabetPosition(text) {
let arr =['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'];
let index = []
for(let i=0;i<text.length;i++){
  for(let j=0;j<arr.length;j++){
    if(/^[A-Za-z]*$/.test(text[i]) && text[i].toLowerCase() === arr[j]){
      index.push(j+1)
    }
  }
}
  return index.join(' ');
}

票數最高的答案:

ex:  alphabetPosition("The twelve o' clock.")

function alphabetPosition(text) {
  return text
    .toUpperCase()                    //  THE TWELVE O' CLOCK.
    .replace(/[^A-Z]/g, '')            //  THETWELVEOCLOCK
    .split('')                         // ["T", "H", "E", "T", "W", "E", "L", "V", "E", "O", "C", "L", "O", "C", "K"]
    .map( (c) => c.charCodeAt() - 64)  // [20, 8, 5, 20, 23, 5, 12, 22, 5, 15, 3, 12, 15, 3, 11]
    .join(' ');                        // 20 8 5 20 23 5 12 22 5 15 3 12 15 3 11
}


思考:

1、我用了一個最笨的方式去確定所有字母對應的數字

2、'a'.charCodeAt()  =》 97: 列印字母"a"的十進位制表示形式

     'A'.charCodeAt()  =》 65: 列印字母"A"的十進位制表示形式

相關推薦

codewars解題筆記 —— 字串匹配

題目 In this kata you are required to, given a string, replace every letter with its position in the alphabet. If anything in the text isn'

codewars解題筆記 —— 字串的篩選和拼接

題目: Your job is to write a function which increments a string, to create a new string. If the string already ends with a number, the numb

codewars解題筆記 —— 將字串轉駝峰,單詞首字母大寫

題目:Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output s

Leet Code 解題筆記——字串中的第一個唯一字元

題目描述: 給定一個字串,找到它的第一個不重複的字元,並返回它的索引。如果不存在,則返回 -1。 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事項:您可以假定該字串只包含小寫字母。 解題思路: #

KMP 字串匹配 學習筆記

寫在前面: 建議大家先去看洛谷P3375這道題,結合題目食用風味更佳 演算法的原型:暴力樸素演算法(不上程式碼了因為我懶):將原串與用於匹配的串一位一位的匹配,時間複雜度\(O_(nm)\) 改進方法:使用一個next陣列來記錄用於匹配的串的這個點之前的串字首與字尾的最大匹配位數 舉個例子: \(S_1

字串匹配之RK演算法——學習筆記

RK演算法是Rabin-Karp演算法的簡稱,是經典的字串匹配演算法,在《演算法導論》上是有介紹的,有興趣的同學可以去看看。 RK演算法的複雜度可以說是比上不足比下有餘,比一般的匹配演算法要好,但是又比不上KMP,Sunday等演算法。演算法表現跟快排比較相似,演算法平均複

字串匹配(刪除指定字元)/codewars練習

Write function scramble(str1,str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returnsfal

Nginx學習筆記04URL匹配規則和實際路徑

oca 定義 wid val style 例如 top font 相同 1.1.1. URL匹配規則 匹配規則配置總結: location [=|~|~*|^~] /uri/ { } 優先級 匹配方式 描述 1最高 = 精確匹配。

解題筆記PalindromeNumber[Easy]

inf pxn aip href pod dir m60 www ddx 2UDx6惱84恫蚊輝雍40http://www.docin.com/inczc305 Ce6m035夯ywhttp://www.docin.com/pvcbu109 423繕yoe悄06固g久衷

LeetCode解題筆記 - 4. Median of Two Sorted Arrays

res pre cnblogs 返回 熱門 median 輸入 cat find There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the

LeetCode解題筆記 - 2. Add Two Numbers

number 不能 add 記錄 += parse ror new [] 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers.

LeetCode解題筆記 - 3. Longest Substring Without Repeating Characters

返回 swe 字符串 opened 比較 obj 是把 character def Given a string, find the length of the longest substring without repeating characters. Examples

Python學習筆記模式匹配與正則表達式之用正則表達式匹配更多模式

重復 實例 int clas span 就是 image 特定 mat 隨筆記錄方便自己和同路人查閱。 #------------------------------------------------我是可恥的分割線--------------------------

Rabin-karp演算法實現 字串匹配

// RabinKarp演算法實現 // RabinKarp演算法實現 const primeRK = 16777619 func hashStr(seq string) (uint32, uint32) { hash := uint32(0) for _, value

LeetCode 解題筆記——有效的數獨

題目描述:判斷一個 9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。 數字 1-9 在每一行只能出現一次。 數字 1-9 在每一列只能出現一次。 數字 1-9 在每一個以粗實線分隔

leetcode 214 Shortest Palindrome kmp演算法 字首字尾字串匹配

0 leetcode 214. Shortest Palindrome  本題的描述是一個串前方加上一些字串,使其成為一個迴文串。 形式類似於(新增部分)(迴文部分)(其餘部分),所以我們的目標就是將其迴文部分求出來,或者把他的長度求出來。 如果用暴力解法,那麼問題就變成

scala筆記-模式匹配(14)

模式匹配 // Scala是沒有Java中的switch case語法的,相對應的,Scala提供了更加強大的match case語法,即模式匹配,類替代switch case,match case也被稱為模式匹配 // Scala的match case與Java的switch ca

字串匹配的RabinKarp演算法的c語言實現

</pre><pre name="code" class="cpp">#include<string.h> int check( char *s1,char *s2,int n ); int main() { char s1[10000],s2[1000000]

Python學習筆記模式匹配與正則表示式之使用和不使用正則表示式

 隨筆記錄方便自己和同路人查閱。 #------------------------------------------------我是可恥的分割線-------------------------------------------   假設你希望在字串中查詢電話號碼。你知道模式:3個數字,一

Luogu P3375 【模板】KMP字串匹配

  題目描述 如題,給出兩個字串s1和s2,其中s2為s1的子串,求出s2在s1中所有出現的位置。 為了減少騙分的情況,接下來還要輸出子串的字首陣列next。 (如果你不知道這是什麼意思也不要問,去百度搜[kmp演算法]學習一下就知道了。) 輸入輸出格式 輸入格式: