4 kyu

Strings Mix

868 of 24,085g964

Description:

Given two strings s1 and s2, we want to visualize how different the two strings are. We will only take into account the lowercase letters (a to z). First let us count the frequency of each lowercase letters in s1 and s2.

s1 = "A aaaa bb c"

s2 = "& aaa bbb c d"

s1 has 4 'a', 2 'b', 1 'c'

s2 has 3 'a', 3 'b', 1 'c', 1 'd'

So the maximum for 'a' in s1 and s2 is 4 from s1; the maximum for 'b' is 3 from s2. In the following we will not consider letters when the maximum of their occurrences is less than or equal to 1.

We can resume the differences between s1 and s2 in the following string: "1:aaaa/2:bbb" where 1 in 1:aaaa stands for string s1 and aaaa because the maximum for a is 4. In the same manner 2:bbb stands for string s2 and bbb because the maximum for b is 3.

The task is to produce a string in which each lowercase letters of s1 or s2 appears as many times as its maximum if this maximum is strictly greater than 1; these letters will be prefixed by the number of the string where they appear with their maximum value and :. If the maximum is in s1 as well as in s2 the prefix is =:.

In the result, substrings (a substring is for example 2:nnnnn or 1:hhh; it contains the prefix) will be in decreasing order of their length and when they have the same length sorted in ascending lexicographic order (letters and digits - more precisely sorted by codepoint); the different groups will be separated by '/'. See examples and "Example Tests".

Hopefully other examples can make this clearer.

s1 = "my&friend&Paul has heavy hats! &"
s2 = "my friend John has many many friends &"
mix(s1, s2) --> "2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss"

s1 = "mmmmm m nnnnn y&friend&Paul has heavy hats! &"
s2 = "my frie n d Joh n has ma n y ma n y frie n ds n&"
mix(s1, s2) --> "1:mmmmmm/=:nnnnnn/1:aaaa/1:hhh/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss"

s1="Are the kids at home? aaaaa fffff"
s2="Yes they are here! aaaaa fffff"
mix(s1, s2) --> "=:aaaaaa/2:eeeee/=:fffff/1:tt/2:rr/=:hh"

Note for Swift, R, PowerShell

The prefix =: is replaced by E:

s1 = "mmmmm m nnnnn y&friend&Paul has heavy hats! &"
s2 = "my frie n d Joh n has ma n y ma n y frie n ds n&"
mix(s1, s2) --> "1:mmmmmm/E:nnnnnn/1:aaaa/1:hhh/2:yyy/2:dd/2:ff/2:ii/2:rr/E:ee/E:ss"
Fundamentals
Strings

More By Author:

Check out these other kata created by g964

Stats:

CreatedOct 23, 2015
PublishedOct 23, 2015
Warriors Trained83698
Total Skips26015
Total Code Submissions168176
Total Times Completed24085
Ruby Completions868
Python Completions9623
JavaScript Completions5461
Haskell Completions278
Java Completions2321
Clojure Completions80
CoffeeScript Completions21
C# Completions1450
C++ Completions1148
PHP Completions478
Crystal Completions18
C Completions493
TypeScript Completions495
Rust Completions421
Swift Completions185
Shell Completions31
OCaml Completions25
R Completions70
Kotlin Completions231
F# Completions50
Scala Completions154
Julia Completions30
PowerShell Completions46
Go Completions408
Nim Completions12
Racket Completions17
Reason Completions2
Elixir Completions71
Pascal Completions11
Perl Completions19
Lua Completions55
Elm Completions7
COBOL Completions6
D Completions3
Prolog Completions5
Total Stars2652
% of votes with a positive feedback rating87% of 3222
Total "Very Satisfied" Votes2529
Total "Somewhat Satisfied" Votes546
Total "Not Satisfied" Votes147
Ad
Contributors
  • g964 Avatar
  • jhoffner Avatar
  • alchemy Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • akar-0 Avatar
  • Just4FunCoder Avatar
  • KayleighWasTaken Avatar
Ad