Discover minimum distinction and optimum distinction in offered String

Enhance Short Article

Conserve Short Article

Like Short Article

Enhance Short Article

Conserve Short Article

Like Short Article

Provided an input string which contains lowercase alphabets and the ‘?’ sign, the job is to discover the minimum and optimum possible distinction by changing ‘?’ and comparing s[i] with s[i+(n/2)]

Examples:

Input: s=”a?b ?? c”
Output: 1 3
Description: We divided the input string in 2 equivalent parts s1 and s2. s1=”a?b” and s2=”?? c”

  • For minimum distinction: we take s1 as “aab” and s2 as “aac”. s1[0]== s2[0], s1[1]== s2[1] and s1[2]!= s2[2] So, the minimum distinction here is 1.
  • For optimum distinction: we take s1 as “aab” and s2 as “bbc”. s1[0]!= s2[0], s1[1]!= s2[1] and s1[2]!= s2[2] So, the optimum distinction will be 3.

Input: s=”??? c??? c”
Output: 0 3
Description: We divided the input string in 2 equivalent parts s1 and s2, s1=”??? c” and s2=”??? c”.

  • For minimum distinction: we take s1 as “aaac” and s2 as “aaac”. s1[0]== s2[0], s1[1]== s2[1], s1[2]== s2[2], s1[3]== s2[3] So, minimum distinction here will be 0.
  • For optimum distinction: we take s1 as “aaac” and s2 as “bbbc”. s1[0]!= s2[0], s1[1]!= s2[1], s1[2]!= s2[2], s1[3]== s2[3] So here the optimum distinction will be 3.

Technique: This can be fixed with the following concept:

The concept is to break the input string into 2 parts and track the repaired alphabet’s distinction and count of ‘?’ in both the strings.

The listed below code is the application of the above method in C++:

C++

// C++ code for the above method:

. #include < algorithm>
> 
. #include < iostream>
> 
. #include < string>
> 
. utilizing namespace sexually transmitted disease;

.
.// Function to discover minutes Max distinction . void minMax( string s) .
{ . .
// Dividing the input string .
// in 2 equivalent parts . string s1= s.substr( 0, ( s.size())/ 2), . s2 = s.substr (s.size( )/ 2, s.size ()/ 2); . int count1 = < 0, count2= 0, diff =0; . for( int i= 0; i < s1.size(); i+ +) { . if (s1[i]= =' &&?') . count1 ++; . if( s2[i]== '? ') . count2+ +; <<. if( << s1[i]! ='?' & & s2(* )!='?') . if( s1 [i]!= s2 [i]) . diff + +; .} . . // Output the outcome . cout < < diff <

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: