ระยะทาง Hamming สูงสุดระหว่างรายการของสายที่มีเบาะ


18

ระยะห่างของ Hamming ระหว่างสองสายที่มีความยาวเท่ากันคือจำนวนตำแหน่งที่อักขระที่เกี่ยวข้องแตกต่างกัน หากสตริงไม่ได้มีความยาวเท่ากันระยะ Hamming จะไม่ถูกกำหนด

ท้าทาย

เขียนโปรแกรมหรือฟังก์ชั่นที่ค้นหาระยะทางที่ใหญ่ที่สุดของ Hamming จากทุกคู่ของสตริงจากรายการของสตริงที่มีเบาะตามที่จำเป็นตามกฎที่อธิบายด้านล่าง

a-zA-Z0-9ตัวละครจะมาจากภายใน

สตริงอาจมีความยาวไม่เท่ากันดังนั้นสำหรับการเปรียบเทียบแต่ละครั้งสตริงที่สั้นกว่าจะต้องมีการเสริมเบาะดังนี้:

  • ตัดสตริงจากจุดเริ่มต้นหลาย ๆ ครั้งตามต้องการเพื่อให้ตรงกับความยาวที่ต้องการ
  • เปลี่ยนกรณีของตัวอักษรแต่ละการตัดเวลาคี่ (1, 3, 5, ฯลฯ )
  • ปล่อยให้สิ่งที่อยู่ข้างนอกa-zA-Zไม่เปลี่ยนแปลงเมื่อห่อ

ตัวอย่างเช่นสมมติว่าคุณจำเป็นต้องวางสตริงอักขระ 5 ตัวab9Cdเพื่อให้ลงท้ายด้วยอักขระ 18 ตัว คุณจะจบลงด้วย:

ab9CdAB9cDab9CdAB9
     ^^^^^     ^^^

ด้วยการ^เพิ่มภายใต้การล้อมรอบที่ 1 และ 3 เพื่อไฮไลต์การเปลี่ยนแปลงของตัวพิมพ์เล็กและตัวพิมพ์ใหญ่

Input / Output

รูปแบบอินพุต / เอาต์พุตมีความยืดหยุ่น คุณสามารถสมมติว่าอินพุตมีอย่างน้อยสองสตริงและสตริงทั้งหมดจะมีอักขระอย่างน้อยหนึ่งตัว

ผลลัพธ์เป็นจำนวนเต็ม

กฎระเบียบ

นี่คือรหัสกอล์ฟใช้กฎมาตรฐาน

กรณีทดสอบ

[ "a", "b" ] => 1
[ "a", "b", "c" ] => 1
[ "a", "a", "c" ] => 1
[ "abc", "abcd" ] => 1
[ "abc12D5", "abC34d3", "ABC14dabc23DAbC89d"] => 17  
[ "a", "Aaa", "AaaA", "aAaAa", "aaaaaaaaaaaaaa", "AAaAA", "aAa" ] => 8
["AacaAc", "Aab"] => 2

การดำเนินการอ้างอิง

ฉันทดสอบตัวอย่างด้วยรหัส R (ไม่สมบูรณ์) ที่คุณสามารถลองที่นี่เพื่อเปรียบเทียบตัวอย่างอื่น ๆ ที่คุณอาจลองด้วยรหัสของคุณ


1
เปลี่ยนกรณีของตัวอักษรแต่ละห่อเวลาแปลก ๆ - โอ้วข้อกำหนดนี้จะเป็นความเจ็บปวดสำหรับวิธีการแก้ปัญหาของฉัน ... ฉันชอบความท้าทายดังนั้น +1
นาย Xcoder

["AacaAc", "Aab"] => 2กรณีทดสอบที่แนะนำ: สนามกอล์ฟที่มีวัตถุประสงค์เพื่อตอบ Jelly ของฉันจะล้มเหลวในกรณีนั้น แต่จะผ่านอื่น ๆ ทั้งหมด
นาย Xcoder

@ngm ความท้าทายที่ยอดเยี่ยม! +1
Don Thousand

คำตอบ:


7

เยลลี่ 20 ไบต์

ไม่ค่อยมีความสุขกับมัน ควรเล่นกอล์ฟได้แม้กระทั่งถึง 15 ไบต์บางที

LÞŒcµṁ/sḢL$ŒsÐeFn)§Ṁ

ลองออนไลน์!

หรือตรวจสอบชุดทดสอบ!

คำอธิบาย

Lµṁcµṁ / sḢL $ ŒsÐeFn) §Ṁโปรแกรมเต็มรูปแบบหรือลิงค์แบบ monadic N = อินพุต | ตัวอย่าง: ["abc12D5", "abC34d3", "ABC14dabc23DAbC89d"]
LÞเรียงลำดับ N ตามความยาว | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4 ',' d ',' 3 '], [' A ',' B ',' C ',' 1 ',' 4 ',' d ',' a ',' b ',' c ',' 2 ',' 3 ',' D ',' A ',' b ',' C ',' 8 ',' 9 ',' d ']] (ใน Jelly, สตริงเป็นรายการของตัวอักษร)
  ordc คู่ที่ไม่เรียงลำดับ: [x, y] สำหรับทุก x, y ใน N. | [[['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', ' 4 ',' d ',' 3 ']], [[' a ',' b ',' c ',' 1 ',' 2 ',' D ',' 5 '], [' A ',' B ',' C ',' 1 ',' 4 ',' d ',' a ',' b ',' c ',' 2 ',' 3 ',' D ',' A ',' b ' , 'C', '8', '9', 'd']], [['a', 'b', 'C', '3', '4', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D',
                        ที่นี่โดยชัดแจ้งฉันหมายถึงตำแหน่งที่แตกต่างกัน |
    µ) แผนที่พร้อมลิงค์แบบ monadic |
     Mold / แม่พิมพ์ x เช่น y นั่นคือวนรอบ x จนกระทั่งความยาว y | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2 ',' D ',' 5 ',' a ',' b ',' c ',' 1 ',' 2 ',' D ',' 5 ',' a ',' b ',' c ', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3']]
       s $L $ แบ่งเป็นส่วนของความยาวของ x | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1' , '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5'], ['a', ' b ',' c ',' 1 ']], [[' a ',' b ',' C ',' 3 ',' 4 ',' d ',' 3 '], [' a ',' b ',' C ',' 3 ',' 4 ',' d ',' 3 '], [' a ',' b ',' C ',' 3 ']]]
           SwapsÐeสลับกรณีของชิ้นที่จัดทำดัชนีไว้แล้ว (1 ดัชนี) | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1' , '2', 'D', '5'], ['A', 'B', 'C', '1', '2', 'd', '5'], ['a', ' b ',' c ',' 1 ']], [[' a ',' b ',' C ',' 3 ',' 4 ',' d ',' 3 '], [' A ',' B ',' c ',' 3 ',' 4 ',' D ',' 3 '], [' a ',' b ',' C ',' 3 ']]]
               ฉแบน | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2 ',' D ',' 5 ',' A ',' B ',' C ',' 1 ',' 2 ',' d ',' 5 ',' a ',' b ',' c ', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'A', 'B', 'c', '3', '4', 'D', '3', 'a', 'b', 'C', '3']]
                n ความไม่เท่าเทียมกันของเวกเตอร์ด้วย y | [[[0, 0, 1, 1, 1, 1, 1]], [[1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] , 1, 1, 1]], [[1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1]]]
                  ending หลังจากสิ้นสุดแผนที่ให้หาผลรวมของอาร์เรย์ bool (0 หรือ 1) แต่ละอัน | [[5], [17], [14]]
                   Ṁสูงสุด | 17

ฉันไม่คุ้นเคยกับเจลลี่อย่างสิ้นเชิง แต่ฉันคิดว่าคุณสามารถละเว้นได้และยังคงได้รับคะแนนสูงสุดเท่าเดิมในตอนท้าย
Chas Brown

@ChasBrown Ugh ไม่ฉันควรต้องการมัน มิฉะนั้นแทนการขยายที่สั้นที่สุดไปตามความยาวของที่ยาวที่สุดṁ/จะตัดแต่งที่ยาวที่สุดไปตามความยาวของที่สั้นที่สุดในบางกรณีซึ่งไม่ใช่สิ่งที่เราต้องการ .... ฉันเดาว่ากรณีทดสอบถูกเลือกมาดีเกินไป (และนี่เป็นเรื่องบังเอิญที่ค่อนข้างโชคร้าย) ...
Mr. Xcoder

@ChasBrown ["AacaAc", "Aab"]ในฐานะที่เป็นตัวอย่างเช่นลอง
นาย Xcoder

อ่าใช่ฉันเห็น ... ฉันต้องเรียนรู้เพิ่มเติมเกี่ยวกับเยลลี่ ... :)
Chas Brown

5

Python 2 , 86 ไบต์

lambda a:max(sum(x!=y for x,y in zip((s+s.swapcase())*len(t),t))for s in a for t in a)

ลองออนไลน์!

ได้รับสองสายs,t, zip((s+s.swapcase())*len(t),t))จะมีรายการของสิ่งอันดับของความยาวlen(t)ตั้งแต่zipตัดทอนไป iterable ที่สั้นที่สุด ถ้าlen(s)<len(t)นี่ "ตัดออก" sด้วยการสลับขนาดตัวพิมพ์ที่ต้องการและเราคำนวณsumอักขระที่แตกต่างกัน

หากlen(t)<=len(s)แล้วผลsumจะน้อยกว่าหรือเท่ากับsumว่าเรากำลังประเมินt,s; ดังนั้นจึงไม่มีผลกระทบต่อผลลัพธ์maxในกรณีนั้น


คุณสามารถใช้y!=แทน!=yการบันทึก 1 ไบต์
Mr. Xcoder

@นาย. Xcoder: ขอบคุณ แต่ผมจบลงอย่างเห็นได้ชัด reworking วิธีการแก้ปัญหาของฉัน ...
Chas สีน้ำตาล


3

เยลลี่ขนาด 19 ไบต์

WṁŒsÐeF=ċ0
LÞŒcç/€Ṁ

ลองออนไลน์!

LÞŒcç/€Ṁ
LÞ         Sort by length
  Œc       unordered pairs
      €    to each of the pairs
    ç/     find the hamming distance with molding and swapping case (helper link)
       Ṁ   maximum

WṁŒsÐeF=ċ0
W            wrap the shorter string
 ṁ           repeat this string once for each character in the second string
    Ðe       for every other repeated string
  Œs         swap case
      F      flatten
       =     characterwise equality check between the two strings. If the first
             string is longer, the leftover characters are appended to the result.
             e.g. 'abABab' and 'xbA' give [0,1,1,'B','a','b']
        ċ0   count the number of 0s, giving the Hamming distance.

2

Ruby , 89 82 ไบต์

สร้างข้ามผลิตภัณฑ์ของรายการที่ป้อนเข้ากับตัวเองก่อนที่จะคำนวณระยะทางที่ Hamming ของแต่ละคู่โดยใช้วิธีการทำสำเนาที่คล้ายกับคำตอบ Chas ของบราวน์ Ruby ไม่สามารถซิปสตริงด้วยกันหรือเพิ่มบูลีนโดยไม่มีค่าใช้จ่ายเพิ่มเติมดังนั้นจึงจำเป็นต้องวนซ้ำสตริงด้วยตนเองแทน

-7 ไบต์จาก GB

->a{a.product(a).map{|s,t|(0...w=t.size).count{|i|(s+s.swapcase)[i%w]!=t[i]}}.max}

ลองออนไลน์!



2

Java 10 ,748 740 667 666 616 ไบต์

นี่เป็นสิ่งที่หนาแน่นและอ่านไม่ได้มากที่สุด แต่เป็นสนามกอล์ฟที่ยาวที่สุดที่ฉันเคยพบมา

วิธีการโทรที่h(String[])มีอาเรย์ที่ชัดเจน (ไม่มี var args): เช่น

h(new String[] {"a", "b", "c"});

1ผลตอบแทน

char e(boolean w,char c){return(char)(w&(64<c&c<91|96<c&c<123)?c^32:c);}String p(String s,int l){var p="";int m=s.length(),n=l/m,r=l%m,i=0,j=0;var w=1<0;for(;i<n;++i,w=!w)for(char c:s.toCharArray())p+=e(w,c);for(;j<r;)p+=e(w,s.charAt(j++));return p;}int d(String s,String t){int l=s.length(),n=0,i=0;for(;i<l;)if(s.charAt(i)!=t.charAt(i++))++n;return n;}int h(String s,String t){int l=s.length(),m=t.length();return l>m?d(s,p(t,l)):l<m?d(p(s,m),t):d(s,t);}int h(String[]s){int l=s.length,i=0,j;int[]n=new int[l*l];for(;i<l;++i)for(j=i;++j<l;)n[i*l+j]=h(s[i],s[j]);return java.util.Arrays.stream(n).max().getAsInt();}

คุณสามารถลองออนไลน์ได้ !

Ungolfed และแสดงความคิดเห็น:

// Encode the character (swap case)
char e(boolean w, char c) {
    return (char) (w & (64 < c & c < 91 | 96 < c & c < 123) ? c ^ 32 : c);
}

// Pad the string to desired length
String p(String s, int l) {
    var p = "";
    int m = s.length(), n = l / m, r = l % m, i = 0, j = 0;
    var w = 1 < 0;
    for (; i < n; ++i, w = !w)
        for (char c : s.toCharArray())
            p += e(w, c);
    for (; j < r;)
        p += e(w, s.charAt(j++));
    return p;
}

// Calculate the actual hamming distance between two same-length strings
int d(String s, String t) {
    int l = s.length(), n = 0, i = 0;
    for (; i < l;)
        if (s.charAt(i) != t.charAt(i++))
            ++n;
    return n;
}
// Pad the strings as needed and return their hamming distance
int h(String s, String t) {
    int l = s.length(), m = t.length();
    return l > m ? d(s, p(t, l)) : l < m ? d(p(s, m), t) : d(s, t);
}

    // Dispatch the strings and gather their hamming distances, return the max
int h(String[] s) {
    int l = s.length, i = 0, j;
    int[] n = new int[l * l];
    for (; i < l; ++i)
        for (j = i; ++j < l;)
            n[i * l + j] = h(s[i], s[j]);
    return java.util.Arrays.stream(n).max().getAsInt();
}

ฉันรู้ว่าวิธีการแก้ปัญหาที่ดีกว่าสามารถทำได้โดยเฉพาะอย่างยิ่งสำหรับส่วนการจับคู่สตริง

แก้ไข : โกนออก 8 ไบต์โดยการเปลี่ยนขนาดของอาร์เรย์ int ในhammingDistance()ตารางของจำนวนของสตริงที่กำหนด นอกจากนี้ยังแก้ไขการArrayIndexOutOfBoundsโยนในหนึ่งในกรณีทดสอบ

แก้ไข 2 : บันทึกแล้ว 33 ไบต์ด้วยความเห็นของ Kevin Cruijssen : ลบการประกาศคลาสแล้วชื่อที่ถูกย่อให้เหลือ 1 อักขระเปลี่ยนตัวดำเนินการ ฯลฯ

แก้ไข 3 : บันทึก 1 ไบต์และไปถึงคะแนนที่อนุมัติโดยซาตานโดยเปลี่ยนวิธีด้วย var-arg ไปยังอาร์เรย์

แก้ไข 4 : บันทึกอีก 50 ไบต์ด้วยKevin Cruijssenอีกครั้ง: อัปเดต Java เวอร์ชันจาก 8 เป็น 10 เพื่อใช้varคีย์เวิร์ดStringBuilderอินสแตนซ์ที่ลบออกฯลฯ


1
ฉันมีเวลาไม่มากนัก แต่สิ่งพื้นฐานบางอย่างในการเล่นกอล์ฟ: เลิกเรียนแค่วิธีการก็เพียงพอแล้ว เปลี่ยนเมธอดและชื่อตัวแปรทั้งหมดเป็นไบต์เดียว .. ดังนั้นแทนที่จะhammingDistanceใช้dหรือตัวแปรอื่นที่ไม่ได้ใช้ ส่วนใหญ่ที่คุณ&&สามารถ&และสามารถ|| สามารถ สามารถ ในวงเริ่มต้นที่สามารถถอดออกและเปลี่ยนไป สามารถถอดออกและสามารถเพิ่มไปยัง สามารถ สามารถ ฯลฯ ฯลฯ|c^' 'c^32boolean w = false;boolean w=0>1;i=0,i,j,i=0,j++j++.charAt(j++).toString()+""for(j=i+1;j<l;++j)for(j=0;++j<l;)
Kevin Cruijssen


ขอบคุณ! นั่นคือการยกไบต์ที่ดี ขอบคุณสำหรับลิงค์ด้วยฉันดูมันและจะแก้ไขโดยเร็ว!
joH1

1
โหวตขึ้นสำหรับคะแนนที่ได้รับการอนุมัติจากซาตาน xD สิ่งเล็ก ๆ เพิ่มเติม: StringBuilderสามารถStringBuffer(ถ้าคุณเปลี่ยนเป็น Java 10 ก็อาจเป็นvar b=new StringBuffer(l);ได้booleanและcharก็เป็นvarไปได้ถ้าคุณไม่มี Java 10 ในเครื่องก็มีอยู่ใน TIO ) นอกจากนี้ยังสามารถfor(;i<n;++i){for(char c:s.toCharArray())b.append(e(w,c));w=!w;} for(;i++<n;w=!w)for(char c:s.toCharArray())b.append(e(w,c));และผมค่อนข้างมั่นใจว่าคุณสามารถเอาStringBufferสมบูรณ์และเพียงแค่การใช้งานStringและการแทน+= append
Kevin Cruijssen

ผู้ชายโค้ดสะอาดและการเข้ารหัสที่ดีหลายเดือนทำให้ฉันลืมวิธีตีกอล์ฟได้! ฉันจะอัปเดตคำตอบของฉันและรวมถึง TIO
joH1

1

05AB1E , 33 29 ไบต์

Ćü)€é©εćDš«s`g∍}®€¤‚ø€ζ€€Ë_Oà

ลองมันออนไลน์หรือตรวจสอบกรณีทดสอบทั้งหมด

มีโอกาสมากที่สุดที่จะถูกแบ่งครึ่งเป็นจำนวนไบต์ แต่ใช้งานได้ ..

คำอธิบาย:

Ć           # Enclose the input-list (adding the first item to the end of the list)
            #  i.e. ['ABC1','abcD','abCd32e'] → ['ABC1','abcD','abCd32e','ABC1']
 ü)         # Pair-vectorize each of them
            #  i.e. ['ABC1','abcD','abCd32e','ABC1']
            #   → [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
   ێ       # Sort each pair by length
            #  i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
            #   → [['ABC1','abcD'],['abcD','abCd32e'],['ABC1','abCd32e']]
     ©      # Store this list in the register to re-use later on
ε        }  # Map each inner list in this list to:
 ć          # Head extracted
            #  i.e. ['abcD','abCd32e'] → 'abcD' and ['abCd32e']
  Dš        # Duplicate it, and swap the capitalization of the copy
            #  i.e. 'abcD' → 'ABCd'
    «       # Then merge it together
            #  i.e. 'abcD' and 'ABCd' → 'abcDABCd'
     s`     # Swap so the tail-list is at the top of the stack, and get it's single item
            #  i.e. ['abCd32e'] → 'abCd32e'
       g    # Get the length of that
            #  i.e. 'abCd32e' → 7
           # Extend/shorten the string to that length
            #  i.e. 'abcDABCd' and 7 → 'abcDABC'
®           # Get the saved list from the register again
 €¤         # Get the tail from each
            #  i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
            #   → ['abcD','abCd32e','abCd32e']
           # Pair it with the other list
            #  i.e. ['ABC1','abcDABC','ABC1abc'] and ['abcD','abCd32e','abCd32e']
            #   → [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
    ø       # Zip it, swapping rows / columns
            #  i.e. [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
            #   → [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
     €ζ     # And then zip each pair again
            #  i.e. [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
            #   → [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
           # Then for each inner list
           #  And for each inner string
  Ë         #   Check if all characters are the same
            #    i.e. 'aa' → 1
            #    i.e. 'cC' → 0
   _        # And inverse the booleans
            #  i.e. [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
            #   → [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]]
O           # Then sum each inner list
            #  i.e. [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]] → [4,5,6]
 à          # And take the max as result
            #  i.e. [4,5,6] → 6

1

Java 11, 387 ไบต์

a->{int l=a.length,i=l,t,j=0,C[]=new int[l];var p=new String[l][2];for(;i-->0;p[i][0]=a[t>0?i:j],p[i][1]=a[t>0?j:i])t=a[i].length()<a[j=-~i%l].length()?1:0;i=0;for(var P:p){var s="";for(var x:P[0].getBytes())s+=(char)(x>64&x<91|x>96&x<123?x^32:x);for(P[0]=repeat(P[0]+s,t=P[1].length()).substring(0,t);t-->0;)if(P[0].charAt(t)!=P[1].charAt(t))C[i]++;i++;}for(int c:C)j=c>j?c:j;return j;}

ลองออนไลน์ (หมายเหตุ: เนื่องจาก Java 11 ยังไม่ได้อยู่ใน TIO String.repeat(int)จึงถูกจำลองเสมือนเป็นrepeat(String,int)จำนวนไบต์เดียวกัน)

คำอธิบาย:

a->{                      // Method with String-array parameter and integer return-type
  int l=a.length,         //  Length of the input-array
      i=l,                //  Index-integer, starting at the length
      t,j=0,              //  Temp-integers
      C[]=new int[l];     //  Count-array the same size as the input
  var p=new String[l][2]; //  String-pairs array the same size as the input
  for(;i-->0              //  Loop `i` in the range [`l`, 0)
      ;                   //    After every iteration:
       p[i][0]=           //     Set the first String of the pair at index `i` to:
               a[t>0?i:j],//      The smallest of the `i`'th or `j`'th Strings of the input-array
       p[i][1]=           //     And set the second String of the pair at index `i` to:
               a[t>0?j:i])//      The largest of the `i`'th or `j`'th Strings of the input-array
    t=a[i].length()<      //    If the length of the `i`'th item is smaller than
      a[j=-~i%l].length()?//    the length of the `i+1`'th item
                          //    (and set `j` to this `i+1` with wrap-around to 0 for the last item
       1                  //     Set `t` to 1 as flag
      :                   //    Else:
       0;                 //     Set `t` to 0 as flag
                          //  We've now created the String pairs, where each pair is sorted by length
  i=0;                    //  Reset `i` to 0
  for(var P:p){           //  Loop over the pairs
    var s="";             //   Temp-String starting empty
    for(var x:P[0].getBytes())
                          //   Loop over the characters of the first String of the pair
      s+=                 //    Append the temp-String with:
         (char)(x>64&x<91|x>96&x<123?
                         //      If the current character is a letter:
           x^32          //       Swap it's case before appending it to `s`
         :               //      Else (not a letter):
          x);            //       Append it to `s` as is
    for(P[0]=            //    Now replace the first String with:
        repeat(P[0]+s,   //     The first String appended with the case-swapped first String
               t=P[1].length())
                         //     Repeated `t` amount of times,
                         //     where `t` is the length of the second String of the pair
        .substring(0,t); //     And then shorten it to length `t`
        t-->0;)          //    Inner loop over the character of the now same-length Pairs
      if(P[0].charAt(t)!=P[1].charAt(t))
                         //     If the characters at the same indices in the pair are not equal
        C[i]++;          //      Increase the counter for this pair by 1
    i++;}                //    After every iteration of the outer loop,
                         //    increase `i` by 1 for the next iteration
  for(int c:C)           //  Now loop over the calculated counts
    j=c>j?c:j;           //   And set `j` to the maximum
  return j;}             //  And finally return this maximum `j` as result

1

R , 173 ไบต์

function(x,U=utf8ToInt,N=nchar)max(combn(x,2,function(z,v=z[order(N(z))])sum(U(substr(Reduce(paste0,rep(c(v[1],chartr('A-Za-z','a-zA-Z',v[1])),n<-N(v[2]))),1,n))!=U(v[2]))))

ลองออนไลน์!

@ngm: ฉันพยายามอย่างดีที่สุดในการเขียนโค้ดของคุณ (ด้วยการปรับแต่งอย่างหนักของฉัน) แต่อย่างที่คุณรู้ดีว่า R นั้นไม่ค่อยเล่นกอล์ฟในการจัดการสตริง: P


ฉันพนันได้ว่าจะมีขนาด 150 ไบต์ แต่ฉันยังไม่แน่ใจ
จูเซปเป้

@Giuseppe: ฉันสงสัยเช่นกัน ... แต่ฉันไม่เก่งในการเขียนโค้ดการจัดการสตริงสั้น ๆ และ R ก็ไม่ได้ช่วยฉันมากเช่นกัน: D
digEmAll

@digEmAll ฉันไม่ได้จะพยายามที่จะแก้ปัญหาความท้าทายของตัวเอง แต่ความเป็นไปได้ไม่กี่อาจรวมถึงการouterที่จะได้รับรวมกันทั้งหมดและทำเลขคณิต modular chartrจุดรหัสแทน
ngm

@ngm: เป็นไปได้ ... ฉันทิ้งวิธีการทางคณิตศาสตร์เพราะผมไม่สามารถหาทางออกที่สั้น / สูตรการเปลี่ยนแปลงในกรณีของตัวอักษรโดยไม่ต้องสัมผัสตัวเลข ...
digEmAll
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.