4 kyu

Strings Mix

2,325 of 24,158g964

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 Trained83868
Total Skips26053
Total Code Submissions168402
Total Times Completed24158
Ruby Completions870
Python Completions9656
JavaScript Completions5472
Haskell Completions280
Java Completions2325
Clojure Completions81
CoffeeScript Completions22
C# Completions1454
C++ Completions1164
PHP Completions480
Crystal Completions19
C Completions496
TypeScript Completions498
Rust Completions423
Swift Completions186
Shell Completions32
OCaml Completions26
R Completions71
Kotlin Completions233
F# Completions51
Scala Completions155
Julia Completions31
PowerShell Completions48
Go Completions410
Nim Completions13
Racket Completions18
Reason Completions3
Elixir Completions72
Pascal Completions12
Perl Completions20
Lua Completions57
Elm Completions8
COBOL Completions7
D Completions4
Prolog Completions6
Total Stars2653
% of votes with a positive feedback rating87% of 3231
Total "Very Satisfied" Votes2537
Total "Somewhat Satisfied" Votes546
Total "Not Satisfied" Votes148
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