I stumbled upon the following problem:
Given a string, return a string made of the first 2 chars (if present), however include first char only if it is 'o' and include the second only if it is 'z', so "ozymandias" yields "oz".
Bellow is my try, but please take in consideration that I am a begginer.
public String startOz(String str) {
if(str.length()>=2 && str.substring(0,2)=="oz")
return "oz";
if(str.length()>=1 && str.substring(0,1)=="o")
return "o";
if(str.length()>=2 && str.substring(1,2)=="z")
return "z";
return "";
}
Unfortunately this code doesn't include all the possibilities, maybe is better understandable with an example, if the input would be: startOz("ozk") then instead of "oz" the output will be just a blank space.. Is there a way to salvage my attempt?