the comparsion of any two string is done on the basics of the assci value ? I know how to extract the assci value of char using - ‘0’ but how to do it with string ?
Biggest number string comparsion
hello @Vikaspal
u have compare fuction for that.
or u can directly use less than,greater than equal to operator for comparision
for example for string a ,b
u can compare then as a < b (here this will evaluate to true if a is lexicographical smaller than b otherwise it will be false)
Actually i trying to solve this problem using one more aproach.
- first we extract the first digit of 0 index value like if 56 and then first digits is 5
- and then form 1 index to n findout the first digits of all the elements if if found any digits is greater than the first i will replace it and move to next index.
- In case if first digits is same like in all the arrrays index i will extrac the second digits and compare with another elments and replace it .
I am able to do the do the replace for the first digits is any element is greater than but not able to findout the solution for second digits in case of same first_digits
yeah u r complicating in both ways (logically as well implementation wise).
for this problem it is simple->
bool cmp(string a,string b){
return a+b > b+a;
}
here we will take a+b and b+a
then then we will compare which one is bigger using > operator (yeah > operator works on string as well)
@aman212yadav after doing the comparsion it will swap for append the string:
like if we have 98 9
then 989, 998 then 998 is lex. greater.
what happened when other digits came let say 7 or something else can u please dry run. becaue in case of integer the element are just swapped out
here also two strings will get swapped incase our comparator return false
for example->
[ “989”,“9”]
in here “989” (a) and “9” (b) wiil be passed to comparator.
a+b= “9899”
b+a=“9989”
is a+b > b+a i.e “9899” > “9989”
no … so our comparaator will return false and becaue it is returning false swapping will happen between these two strings.
after swapping array will look like
[“9”, “989”]
@aman212yadav got it thanks so the complexity is just depends upon the sorting using nlogn of algorithm header file.? and what about when we have to take the input as integer only not string what should i do in that case?
yeah …
then u need to use some math
to combine a and b to form ab or ba
@aman212yadav okay like if we have 30 , 45 as a and b, then i will count no of digits in b means 2 and multiply it by 100, 30*100 + 45 to form the no…
so how to count the digits like count = 0, and divide the no/10 untill become zeros increment the count?
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.