相同的数字字母


19

单词的字母要公平。

他们决定在句子中平均出现相同的次数。

例:

Priorities

会变成:

Ppprrioooritttieeesss

每个字母出现3次,最常见的字母i出现3次。

只要将重复的字母放在相似字母的旁边,就不要紧了。

即:

Pppriooorritttieeesss 可以(“ r”字母)

Ppprioororitttieeesss 不好(“ r”字母)

另一个例子:

invoice

会变成:

innvvooiccee

另一个例子:

Remittance Advice

会变成:

Rrremmmiitttaannncce Adddvvvice

空格,逗号,问号,引号等不被视为挑战的字母。只需要考虑[a-zA-Z]。只要有足够的空间,字母的顺序就应该保持不变。

字母的大小写无关紧要,大写和小写字母均视为相同的字母。也就是说:Pip具有2个“ P”和1个“ I”,所以它将变为Piip

不区分大小写的字母可以是任何形式, Piip=piip=piiP=PiiP

这是


2
我可能建议您使用沙盒应对未来的挑战,以便在将问题发布给主要人员之前,先解决所有细节
乔·金

在给定的示例中,“ rrreeemmmiiitttaaannncccdddvvv”是否可以接受(因为仍然保持不同字母的顺序(定义为az))?(我的果冻答案目前依赖于这种解释是可以的。)
乔纳森·艾伦

1
@JonathanAllan Hmm,尽管我将选择留给OP,但我对此表示高度怀疑。非字母字符(空格)不仅消失了,而且所有字符都相邻放置而不是放在同一位置。您的输出使挑战变得不同且容易(恕我直言)。
凯文·克鲁伊森

1
@KevinCruijssen,空格在左侧-它不是字母,因此不需要遵守“ 字母顺序应保持不变”
Jonathan Allan

1
@JonathanAllan啊,我没注意到空间。我完全理解您在Jelly答案中提供的理由,并基于此理由确实是有效的输出,但是我宁愿看到措辞有所更改,然后允许您的输出,因为这将完全改变挑战本身。
凯文·克鲁伊森

Answers:


5

05AB1E,16个字节

lDáÙSйls¢Zα>×.;

在线尝试!

说明

l                  # convert input to lowercase
 D                 # duplicate
  á                # keep only letters
   Ù               # remove duplicates
    S              # split to list of chars
     Ð             # triplicate
      ¹ls¢         # count the occurrences of each letter in lowercase input
          Zα       # absolute valuue with max occurrence
            >      # increment
             ×     # repeat each unique char that many times
              .;   # replace the first occurrence of the char in lowercase input with this

7

R,106字节

function(s){for(A in L<-LETTERS)s=sub(A,strrep(A,max(x<-+s-+Map(gsub,L,'',s,T))-x[A]--1),s,T);s}
"+"=nchar

在线尝试!

基本R方法:

  • @ J.Doe R + stringr方法中窃取了一些想法,我节省了26个字节!
  • 另一个5个字节使用@ J.Doe建议滥用ř保存+操作者

我印象深刻的是,您的base-R达到了111!
J.Doe

@ J.Doe:发布了我原来的137个字节的解决方案之后,我略微更改了受您启发的方法,并且基本上已经收敛到您的解决方案,只是删除了stringr:D
digEmAll

1
106字节,操作员滥用。Base-R获胜!
J.Doe

@ J.Doe:太棒了!
digEmAll

5

Perl 6,82个字节

-3字节归功于nwellnhof

->\a{a.=lc.=subst($_,$_ x a.comb(/<:L>/).Bag.values.max+1-a.comb($_))for 'a'..'z'}

在线尝试!

接受可变的字符串并将其修改到位。

说明:

->\a{        # Anonymous code block that takes a mutable string            }
 a.=lc;  # Lowercase
                                                               for 'a'..'z'  # For each letter
 .=subst(                                                    )  # Substitute
          $_,   #The first occurrence of the letter with
             $_ x  #The letter repeated
                  a.comb(/<:L>/).Bag.values.max    # The count of the most common letter
                                                 +1  # Plus 1
                                                   -a.comb($_)  # Minus the count of that letter already in the string

您可以.=像那样链接运算符a.=lc.=subst(...)。我不确定是否允许更改现有字母的大小写。也<:L>代替<:Ll>
nwellnhof

@nwellnhof是的,询问者说输出不区分大小写
Jo King

5

JavaScript(ES6),112字节

s=>(m=g=F=>s.replace(/[a-z]/gi,c=>F(c.toLowerCase())))(c=>g[c]=c+c.repeat(m-g[c]),g(c=>m=(n=g[c]=-~g[c])<m?m:n))

在线尝试!

已评论

s => (                       // s = input string
  m =                        // m = max. number of occurrences of the same letter
  g = F =>                   // g = helper function taking a callback function F
    s.replace(               //     (also used to store the # of occurrences of each letter)
      /[a-z]/gi,             //   for each letter c in s:
      c => F(                //     invoke F():
        c.toLowerCase()      //       with c.toLowerCase()
      )                      //     end of call to F()
    )                        //   end of replace()
)(c =>                       // invoke g() (second pass):
  g[c] =                     //   update g[c] to a non-numeric value
    c +                      //   append c once, unconditionally
    c.repeat(m - g[c]),      //   and append c as many times as required to reach m
                             //   (any subsequent iteration with the same letter will
                             //   lead to c.repeat(m - g[c]) --> c.repeat(NaN) --> '')
  g(c =>                     //   invoke g() (first pass):
    m = (n = g[c] = -~g[c])  //     increment g[c], save the result in n
      < m ? m : n            //     and update m to max(m, n)
  )                          //   end of first pass
)                            // end of second pass

我的JS技能很烂,所以我对这部分有些困惑:o[l] = // updates o[l] to a non-numeric value。如果我正确理解,and 函数中是否o有一个整数数组,但是更改为一个字符串数组,该字符串数组在我前面提到的部分保留了一个或多个字符?另外,我猜默认值是,因为您使用而不是?Fgcoundefinedo[l]=-~o[l]++o[l]
凯文·克鲁伊森

1
@KevinCruijssen我们希望每个字母仅填充一次至最大出现次数。通过更新o[l]为字母,具有该字母的任何后续迭代将导致m - o[l] --> NaN(整数减字母)和l.repeat(NaN) == ''。(关于最后一点:是的,没错。)
Arnauld

好的,谢谢您的解释!:)
Kevin Cruijssen

(我应该说字符串而不是字母
Arnauld

5

J33 56 46字节

t=:~:tolower
(#~1+t*~:(*>./-])t*1#.e.)@toupper

在线尝试!

无法找到避免使用的方法 ~:tolower两次。

怎么运行的

t=:~:tolower    Auxiliary function: isupper
     tolower    Is lowercase version of itself...
   ~:           different from itself?

(#~1+t*~:(*>./-])t*1#.e.)@toupper    Main function
                          toupper    Convert to uppercase
                      e.     Build 2D array by comparing to itself
                   1#.       Row-wise sum; Count occurrences
                 t*     A) Filter by isupper (needed for finding max count)
           >./-]        Compute max of A) minus each element of A)
       ~:          Nub sieve; 1 if first occurrence, 0 otherwise
          *        Filter first occurrences only
     t*       Filter by isupper again, to ban non-alphabets from duplicating
   1+         Add one to preserve given chars
 #~           Duplicate

5

R +字符串,108个字节

我不是很擅长stringr。返回小写和大写字母的混合字母,因为该问题表示无关紧要。

function(x){for(l in L<-letters)x=sub(l,strrep(l,max(s<-stringr::str_count(tolower(x),L))-s[L==l]+1),x,T);x}

在线尝试!

说明

function(x){
for(l in letters){ # Iterate through builtin vector "a", "b", "c"...
   # Generate a 26-long integer vector for how many a's, b's, c's in lower case string
  s = stringr::str_count(tolower(x),letters)
    # Take the max of this
  m = max(s)
    # Repeat the letter in the iteration enough times to make the word 'fair'
  new.l = strrep(l,m-s[letters==l]+1)
    # Substitute the first instance only of the letter in the string for the repeated letter
    # This is case insensitive (the T at the end)
    # Notice we calculate the max letter frequency each loop
    # This is inefficient but doesn't change the answer and avoids bytes
  x=sub(l,new.l,x,T);
  }
x # Return the substituted string
}

3

K4,35个字节

解:

{x@o@<o:(&^x),/(|/#:'g)#'g:" "_=_x}

例子:

q)k){x@o@<o:(&^x),/(|/#:'g)#'g:" "_=_x}"Priorities"
"PPPrrioooritttieeesss"
q)k){x@o@<o:(&^x),/(|/#:'g)#'g:" "_=_x}"invoice"
"innvvooiccee"
q)k){x@o@<o:(&^x),/(|/#:'g)#'g:" "_=_x}"Remittance Notice"
"RRRemmmiittaaanncce Noootice"

说明:

可能会以不同的方式打高尔夫球,会不断思考

{x@o@<o:(&^x),/(|/#:'g)#'g:" "_=_x} / the solution
{                                 } / lambda taking implicit argument x
                                _x  / lowercase input
                               =    / group
                           " "_     / drop space from keys
                         g:         / save as g
                       #'           / take each
               (      )             / do this together
                  #:'g              / count occurances in each group
                |/                  / take the maximum
             ,/                     / flatten with
        (&^x)                       / indices where input is null (ie " ")
      o:                            / save as o
     <                              / indices to sort o ascending
   o@                               / apply these to o
 x@                                 / apply these indices to original input

3

木炭33 32字节

⭆↧θ⁺§θκ×ι∧№βι∧⁼κ⌕↧θι⁻⌈Eβ№↧θλ№↧θι

在线尝试!链接是详细版本的代码。说明:

  θ                                 Input string
 ↧                                  Lower case
⭆                                   Map over characters and join
      κ                             Current index
     θ                              Input string
    §                               Original character
   ⁺                                Concatenate with
        ι                           Lowercased character
       ×                            Repeated
            ι                       Lowercased character
           β                        Lowercase alphabet
          №                         Count
         ∧                          Logical And
                   ι                Lowercased character
                  θ                 Input string
                 ↧                  Lower case
                ⌕                   Find
               κ                    Current index
              ⁼                     Equals
             ∧                      Logical And
                       β            Lowercase alphabet
                      E             Map over characters
                           λ        Current character
                          θ         Input string
                         ↧          Lower case
                        №           Count
                     ⌈              Maximum
                    ⁻               Minus
                               ι    Lowercased character
                              θ     Input string
                             ↧      Lower case
                            №       Count
                                    Implicitly print

3

爪哇11,190个 176 162字节

s->{s=s.toUpperCase();char m=2,i=64,a[]=new char[127];for(int c:s.getBytes())m-=m+~++a[c]>>-1;for(;++i<91;)s=s.replaceFirst(i+"",repeat((i+""),m-a[i]));return s;}

-14个字节感谢@Nevay

输出为全大写。

在线尝试。(注意:String.repeat(int)因为repeat(String,int)相同的字节数而被仿真,因为Java 11尚未在TIO上。)

说明:

s->{                      // Method with String as both parameter and return-type
  s=s.toUpperCase();      //  Convert the input-String to full uppercase
  char m=2,               //  Max occurrence (+1), starting at 2
       i=64,              //  Index integer, starting at 64 ('A'-1)
       a[]=new char[127]; //  Create a count-array of size 127 (printable ASCII chars)
  for(int c:s.getBytes()) //  Loop over the characters of the String as integers
    m-=m+~++a[c]>>-1;     //   Increase the occurrence-counter of the char by 1 first
                          //   And if it's larger than the max-2, increase the max by 1
  for(;++i<91;)           //  Loop `i` in the range ['A', 'Z']
    s=s.replaceFirst(i+"",//   Replace the first char `i` in the string with:
       (i+"").repeat(     //   That same character repeated
        m-a[i]));         //   The max(+1) minus its array-occurrence amount of times
  return s;}              //  Then return the now modified String as result

可以将var用作字节吗?
Quintec '18

@Quintec不是char您的意思?很不幸的是,不行。var只能用于单个字段。所以代替char m=1,i=127,a[]=new char[i];var m=1;var i=127;var a=new char[i];这是有关Java 10可以做什么和不能做什么的有用提示var(我可以用替换int循环中的var,但字节数将保持不变。)
凯文·克鲁伊森

知道了,谢谢。仍然不知道Java 9/10/11是如何工作的,哈哈,我会坚持使用8; p
Quintec

@Quintec Java 9我也不太了解,因为它主要集中在REPL上。Java 10与Java 10基本相同,除了var。Java 11在所有与代码高尔夫相关的方面几乎没有任何更改,除了String.repeat我已经使用过很多次的方法。它还具有新的String.stripLeadingString.stripTrailing,其作用类似于trim但仅前导/尾随空格,并且String.isBlank()String.trim().isEmpty()(仅空或空白)相同。
凯文·克鲁伊森

1
-14个字节:s->{s=s.toUpperCase();char m=2,i=91,a[]=new char[127];for(int c:s.getBytes())m-=m+~++a[c]>>-1;for(;i-->65;)s=s.replaceFirst(i+"",repeat((i+""),m-a[i]));return s;}
Nevay

3

Japt -h,27个字节

@ETHproductions中的-3个字节

;v
ñ oC ó¥ ú £=iXÎpXèS)UbXg

试图解释

;v                          Convert implicit input to lowercase
ñ oC ó¥ ú £=iXÎpXèS)UbXg      Main function. Implicit lowercase input => "priorities"
ñ                           Sort => "eiiioprrst"
 oC                         Remove non alphabetical chars
   ó¥                       Split on different letters => ["e","iii","o","p","rr","s","t"]
     ú                      Right-pad each to the length of the longest with space => ["e  ","iii","o  ","p  ","rr ","s  ","t  "]
       £                    For each X in this array:
             XèS              Count the number of spaces in X
          XÎ                  Get the first character in X
            p   )             Repeat it (number of spaces) times
                              example the mapped value "e  " will become "ee"
         i                    Insert this into U at
                 UbXg           the first index of (first character in X) in U
        =                     Set U to the result

在线尝试!


1
希望您不要介意,我扩展了部分解释(该行同时解释了10个字符:P),ú诀窍是天才,顺便说一句:-)
ETHproductions '18

@ETHproductions,我很感激。我的英语不太好,所以谢谢
Luis felipe De jesus Munoz'Oct

1
不幸的是,当涉及非字母时,它似乎失败了(它们不应更改)。一个简单的解决方法是插入ñ oC ó¥,尽管它需要重新添加;...
ETHproductions '18

等等...什么时候开始ñ对字符串进行处理?@ETHproductions,请告诉我这是最近添加的内容,我一直没有忽略它!
毛茸茸的

@Shaggy显然是在2.5个月前的 -但是不用担心,即使我忘记了它也要等到这个答案才存在;-)
ETHproductions '18 October 6'2

2

红宝石,89字节

->s{1while(a=s.scan /\w/).map(&g=->x{s.scan(/#{x}/i).size}).uniq[1]&&s[a.min_by &g]*=2;s}

在线尝试!

我尝试了不同的方法,但真正节省大量字节的是一次添加一个字符。

怎么样:

->s{
    1while                             # 1 is a nop to the while
    (a=s.scan /\w/)                    # For all the letters in the string
    .map(&g=->x{s.scan(/#{x}/i).size}) # Count occurrences ignoring case.
    .uniq[1]                           # Break out of loop if all equals
    &&s[a.min_by &g]*=2                # Otherwise duplicate the letter
                                       #  with the lowest count
    ;s}                                # Return the string

2

Powershell 6,123个字节

它使用一个char范围'a'..'z'。请参阅下面有关先前Powershell的脚本。

param($s)for(;'a'..'z'|%{
if($d=($s-replace"[^$_]").Length-$n){if($d-gt0){1}else{$s=$s-replace"^(.*$_)","`$1$_"}}}){$n++}$s

解释测试脚本:

$f = {

param($s)                               # a parameter string
for(;                                   # loop while exists at least one letter...
'a'..'z'|%{                             # for each letter
    $d=($s-replace"[^$_]").Length-$n    # let $d is a difference between a number of current letter and current $n 
    if($d-gt0){                         # if the difference > 0
        1                               # then return a object to increase $n on next iteration
    }
    if($d-lt0){                         # if the differenct < 0
        $s=$s-replace"^(.*$_)","`$1$_"  # append the current letter after a last instance of the letter. Use "^(.*?$_)" regexp to append it after a first instance of the letter.
    }
}){
    $n++                                # increment $n if exists at least one letter number of witch greather then $n
}                                       # and make next iteration of the 'for'.

$s                                      # return modified string if all letters in the string occur the same number of times

}

@(
    ,('Priorities', 'Ppprrioooritttieeesss', 'PPPriooorritttieeesss')
    ,('invoice', 'innvvooiccee')
    ,('Remittance Advice', 'Rrremmmiitttaannncce Adddvvvice', 'RRRemmmitttannnce Aadddvvviicce')
) | % {
    $s,$e = $_
    $r = &$f $s
    "$($r-in$e): $r"
}

输出:

True: Pppriooorritttieeesss
True: innvvooiccee
True: Rrremmmitttannnce Aadddvvviicce

Powershell 5.1-,133字节

param($s)for(;97..122|%{$_=[char]$_
if($d=($s-replace"[^$_]").Length-$n){if($d-gt0){1}else{$s=$s-replace"^(.*$_)","`$1$_"}}}){$n++}$s

2

红色,252字节

func[s][a: charset[#"a"-#"z"#"A"-#"Z"]t: parse s[collect[any[keep a | skip]]]m: copy
#()foreach c t[c: form c either n: m/:c[m/:c: n + 1][m/:c: 1]]d: last sort extract next
to-block m 2 foreach c s[prin c: form c if n: m/:c[loop d - n[prin c]m/:c: d]]]

在线尝试!

荒谬的长期解决方案...

说明:

f: func [ s ] [
    a: charset [ #"a" - #"z" #"A" - #"Z" ]   ; letters
    t: parse s [                             ; parse the string 
        collect [ any [ keep a | skip ] ]    ; and keep only the letters
    ]
    m: copy #()                              ; initialize a map
    foreach c t [                            ; for each character in t
        c: form c                            ; the character as a string
        either n: select m c [ m/:c: n + 1 ] ; increase the count if already in map
                             [ m/:c: 1 ]     ; otherwise create a map entry with count 1 
    ]
    d: last sort extract next to-block m 2   ; convert the map to a block; extract only the 
                                             ; numbers and take the last of the sorted block
    foreach c s [                            ; for each character in the input
        c: form c                            ; the character as a string
        prin c                               ; print it (with no space nor newline)
        if n: select m c [                   ; if c is a key in the map
            loop d - n [ prin c ]            ; print the character again up to d times 
            m/:c: d                          ; set the count to max (flag it as used)
        ]
    ]
]

2

的JavaScript(Node.js的)140个 137字节

x=>[...x=x.toLowerCase()].map(F=c=>(F[c]=-~F[c],F[c]>w?w=F[c]:w,c),w=0).map(c=>x=x.replace(c,c.repeat(c>'`'&c<'{'?w-F[c]+1:1),F[c]=w))&&x

在线尝试!

对于那些永无止境的附加约束,我的第一个解决方案增加了33个字节。JS吸收了不区分大小写的字符串操作。

-3个字节回来感谢@Arnauld。

说明

x =>                                     // The function.
  [...x = x.toLowerCase()].map(f = c => (// - Iterate among each character...
                                         // - Additional constraint 2
    f[c] = -~f[c],                       //   - Add one to the character counter
    f[c] > w ? w = f[c] : w,             //   - Update the maximum count if necessary
    c                                    //   - Return back the character for the use in
                                         //     the next map function
  ), w = 0)                              // - The counters
  .map(c =>                              // - Iterate again...
    x = x.replace(                       //   - Repeat the first appearance of
      c,                                 //   - Each character
      c.repeat(                          //   - Needed number times
        c > '`' & c < '{'                //   - Additional constraint 1
        ? w - f[c] + 1                   //   - If this is letter, repeat
        : 1                              //   - If not, stay as is
      ),                                 //   - That should've been clearly stated
      f[c] = w                           //   - And set the counter so that no further 
                                         //     replacements are done on this character 
    )                                    //   - (w - f[c] + 1 = 1 in further iterations)
  ) && x                                 // - Return the result

解决方案需要能够处理大小写混合的输入。
毛茸茸的

@Shaggy我认为挑战已在您发表评论后进行了编辑。看起来输出的情况无关紧要。
Arnauld

另一方面,函数必须是可重用的,在这里不是这种情况。
阿诺尔德

@Arnauld哦,我用你有时会看到fS作为临时存储,所以我认为这没关系
Shieru Asakoto

map()回调函数可以安全地用于存储,因为它们是在本地范围内定义的。使用主要功能(已全局定义)更加危险。在这里,您可以使用第一个的回调map(),这使您回到137 bytes
阿纳尔德

2

外壳,15个字节

ḟ§Ë#f√MṘO´πL¹m_

在线尝试!

蛮力,太慢了。

说明

ḟ§Ë#f√MṘO´πL¹m_  Implicit input, say s = "To do"
             m_  Convert to lowercase: t = "to do"
           L¹    Length of s: 5
         ´π      All length-5 combinations of [1..5]:
                   [[1,1,1,1,1], [1,1,1,1,2], [2,1,1,1,1], ..., [5,5,5,5,5]]
        O        Sort them lexicographically:
                   [[1,1,1,1,1], [1,1,1,1,2], [1,1,1,1,3], ..., [5,5,5,5,5]]
      MṘ         For each, replicate letters of t that many times:
                   ["to do", "to doo", "to dooo", ..., "tttttooooo     dddddooooo"]
ḟ                Find the first string that satisfies this:
                   Example argument: x = "tto ddo"
    f√             Letters of x: "ttoddo"
  Ë                They have equal
 § #               number of occurrences in x: true (all have 2).

根本无法获得结果
asmgx

@asmgx该程序真的很慢。长度为8或更长的输入在TIO上似乎超时,因为它会在一分钟后终止计算。如果您等待足够长的时间(对于长度为10的输入,可能需要几个小时),则脱机解释器应给出结果。
Zgarb

2

Perl 6的77 70个字节

{s:i|$($!.min(*{*}).key)|$/$/|until [==] ($!=.lc.comb(/<:L>/).Bag){*}}

在线尝试!

采用GB的插入字符直到所有字符出现相同次数的方法。接收就地修改的字符串。

如果下划线可以像字母一样对待,则正则表达式可以变为/\w/,节省两个字节。

说明

{
                    .lc.comb(/<:L>/).Bag          # Create Bag of letter/count pairs
                ($!=                    )         # Store temporarily in $!
 ... until [==]                          .values  # Until all counts are equal
 s:i|                      |    |                 # Replace (ignoring case)
     $($!.min(*.value).key)                       # letter with minimum count
                            $/$/                  # with itself doubled
}

@JoKing看来您的改进是基于旧版本,然后才发现了{*}窍门。
nwellnhof

所以,.value(s)这就像是捷径吗?整洁,我可能不得不更新一些旧解决方案
Jo King

1

Python 2中97个 117字节

s=input().upper()
S=''
for c in s:S+=c+c*(max(map(s.count,map(chr,range(65,91))))-(S+s).count(c))*('@'<c<'[')
print S

在线尝试!


@JoKing固定,。
TF

1

C(铛)246个 223 220 210 208 193 188字节

编译器标志-DF=;for(i=0;b[i];i++ -DB=b[i](29个字节)

添加了大小写混合的支持。

f(char*c){char m,i,s,*b,a[255]={0};s=asprintf(&b,c)F)B=tolower(B),a[B]++F,a[B]>a[m]?m=B:0)F)a[B]^a[m]?b=realloc(b,s+i),bcopy(&B,b+i+1,s),a[B]++:(m=B);puts(b);}

在线尝试!


1

Pyth,31个 30字节

JeSm/Qd=r0QVQ=tQ=+k*N-J/+kQN)k

在这里尝试

说明

JeSm/Qd=r0QVQ=tQ=+k*N-J/+kQN)k
       =r0Q                        Convert input to lowercase.
JeSm/Qd                            Find the count of the most common character.
           VQ               )      For each character in the input...
             =tQ                   ... remove that character from the input...
                =+k*N-J/+kQN       ... append copies to k until we have enough.
                             k     Output.

1

C(GCC) -175字节

f(char*s){int c[999]={0},i=0,m=0,k,L;while((L=s[i++])&&(k=++c[L<97?L+32:L]))m=k>m?k:m;i=0;while(L=s[i++])for(L=L<97&&L>64?L+32:L,putchar(L);isalpha(L)&&++c[L]<=m;)putchar(L);}

不打高尔夫球

f(char *s) {
  int c[999]={0},i=0,m=0,k,L;                      // Array used like a dictionary, temp vars
  while((L=s[i++])&&(k=++c[L<97?L+32:L]))          // store letter counts
    m=k>m?k:m;                                     // calculate max occurance
  i=0;                                             // reset string index
  while(L=s[i++])                                  // iterate string
    for(L=L<97&&L>64?L+32:L,putchar(L);isalpha(L)&&++c[L]<=m;) // set character L to lowercase if in alphabet, print always once, repeat if in alphabet
      putchar(L);                                  // print character
}

在线尝试!


0

Kotlin Android,413个字节

var l: List<Char> = w.toList().distinct();val h = HashMap<Char, Int>();var x='m';var n=0;for(z in l.indices){var c=0;for (i in 0.rangeTo(w.length-1)){if(l[z]==(w[i]))c++};h.put(l[z],c);if(n<c){n=c}};for(entry in h){h.replace(entry.key,n-entry.value)};var v=ArrayList<Char>();for(i  in 0.rangeTo(w.length-1)){if(h.containsKey(w[i])){for(p in 0.rangeTo(h.get(w[i])!!)){v.add(w[i])};h.remove(w[i])}else{v.add(w[i])}}

在线尝试

说明步骤1->选择不同字符的列表。步骤2->获取字符串中每个字符的计数并选择最大字符频率。步骤3->获取相对于最大字符频率的字符频率差异步骤4->相对于字符串中的位置放置字符。解决快乐!



0

的PHP185 173 170字节

function($s){$m=max($a=count_chars($s=strtolower($s),1));foreach(str_split($s)as$c)$o.=str_repeat($c,($b=$a[$d=ord($c)])!=($a[$d]=$m)&&$d>96&&$d<123?$m-$b+1:1);return$o;}

在线尝试!

取消投放(以及未更改和未优化)。

function f($s) {
    $s = strtolower( $s );
    $a = count_chars( $s, 1 );
    $m = max( $a );
    foreach( str_split( $s ) as $c ) {
        if ( $c < 'a' or $c > 'z') {           // is non a-z
            $n = 1;
        } elseif ( $a[ord($c)] == $m ) {    // already has max number
            $n = 1;
        } else {
            $n = $m - $a[ord($c)] + 1;       // add this many chars
        }
        $o .= str_repeat( $c, $n );
        $a[ord($c)] = $m;                   // has reached the max
    }
    return $o; 
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.