Hi,
Number format exception is coming .Please check.
Number format exception Java doubt
reply…
if we try to parse a string to integer but the string is null,The NumberFormatException is thrown.
-
Example- Integer.parseInt("");
handle that case
use this approach for O(kn)
Make a function remove Kdigits and pass String N and int k as parameter
remove Kdigits(String N,int K)Create StringBuilder sb, inside the Function
due to In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. (We want to delete K char from the String)Create Stack of character S
loop from value i=0 to till i<N.length()
Inside the loop
`while (k > 0 && !s.isEmpty() && s.peek() > n.charAt(i)) {
s.pop();
k–;
}
s.push(ch);
}
Aim to make the smallest number
If s.peek() > n.charAt(i) then remove bigger number
We remove only K Character
If stack is Empty you can’t remove anythingAfter completion of 4th step
Stack based on the basic principle of last-in-first-out,
pop Character from Stack s and add First Index of Sb.until and unless Stack is EmptyAbove loop will not remove K Character in some cases. Then follow these steps
Example N=”1324567” K =3
Then sb = “124567”
if the length of sb > length of String N - K, means Character digits are in Increasing order then remove the K Character from last.
Sb = sb.subString(0,N-k+1)// remove total K character
if sb.charAt(0)==’0’
Sb = sb.subString(1)//remove leading 0’s
If sb.length()==0
Return “0” from function //0 is positive smallest number
Else
Return sb;
Solution Link : https://ide.codingblocks.com/s/191861