Problem:
Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true
Solution:
public Boolean xyzThere(String str) { int len = str.length(); String xyz = "xyz"; Boolean match = false; if (len < 3) return false; for (int i = 0; i < len - 2; i ++) { String temp = str.substring(i, i+3); if (temp.compareTo(xyz) == 0 && i == 0) match = true; else if(temp.compareTo(xyz) == 0 && str.charAt(i-1) != 46) match = true; } return match; }
Bullshit. This is my code:
ReplyDeletepublic boolean xyzThere(String str) {
str = str.replaceAll("(\\.)(xyz)", "");
return str.contains("xyz");
}
Much better, isn't it?
There is always a faster way in java. But the purpose of those simple problems is helping the test taker master critical thinking coding skills.
DeleteStudents learn 10 ways to sort arrays in college despite the fact that arrays can be sorted with Arrays.sort().
I'm not saying always look for the complicated ways to solve problems, this is only when you're practicing.
You say bullshit but your solution isn't correct. For example yours returns the wrong answer for "x.xyzyz".
Deleteahahahah you are right. that guy...
Deletei leave my solution here:
public boolean xyzThere(String str) {
for(int i = 0; i < str.length() - 2; i++) {
if(str.charAt(i) == '.') i++;
else if(str.substring(i, i+3).equals("xyz")) return true;
}
return false;
}
ur literally a legend dude
Deletetook me awhile to understand what you did here but i like it. it does work. also made it one line code ( which I love to do). I also agree with working on critial thinking is key. What I was trying before was not working at all.
Deletei need explanation for it
Deletestr = str.replaceAll("(\\.)(xyz)", "");
yes bull shit has a optimized code ,but please do not follow these predefined API and all,this is for practice purpose ,so try it in a better way with analyzing the problem.
DeleteI can not understand this step
Deletestr = str.replaceAll("(\\.)(xyz)", "");
yes bro, this is much better and thanks
DeleteIt's always better to use a primitive than an Object, so I recommend you change Boolean to boolean.
ReplyDeleteWhere does the 46 come from?
ReplyDeleteIt's ASCII code for ".".
DeleteI used a different method. I present to you the method of brute forceing your code to work.
ReplyDeletepublic boolean xyzThere(String str) {
Random ran = new Random();
x = ran.nextInt(2);
if(x==0)
return true;
return false;
}
Why this doesn`t work with str like "abcxyz", "abc.xyzxyz", "abc.xxyz", 12xyz ?
ReplyDeletepublic boolean xyzThere(String str) {
String xyz = "xyz";
String strReplace = str.replaceAll(".xyz","");
if (strReplace.contains(xyz)){
return true;
} else {
return false;
}
}
public boolean xyzThere(String str) {
DeleteString xyz = "xyz";
String strReplace = str.replaceAll(".xyz","");
if (strReplace.contains(xyz)){
return true;
} else {
return false;
}
//here.. missing return statment
}
public boolean xyzThere(String str) {
ReplyDeleteint a = 0; // counter for dots
int b = 0; // counter for no dots
if (str.length()<3) return false;
for (int i=0; ia;
}
works for each and every case :D
I don't understand what kind of loop this is and what exactly it does?
Delete" for (int i=0; ia;} "
Can somebody please elaborate it for me?
@gaman: Seems to be broken and missing code.
DeleteCtrl + Shift + Esc
DeleteTry this
ReplyDeletepublic boolean xyzThere(String str) {
if(str.startsWith("xyz"))
return true;
for(int i = 1; i < str.length()-2;i++){
if(str.substring(i,i+3).equals("xyz") && str.charAt(i-1) != '.')
return true;
}
return false;
}
Why would you subtract 2 from str.length??
DeleteThis my favorite solution.
Deletepublic boolean xyzThere(String str)
ReplyDelete{
int x = 0;
int y = 0;
if (str.length() < 3 || str.endsWith(".xyz") ||
str.endsWith("xy"))
{
return false;
}
if (str.startsWith("xyz"))
{
return true;
}
return true;
}
yehh.
ReplyDeletecould u answer?
i can't understand why did u put // before . and separate it from xyz
and why it's wrong to write it (".xyz")
Deletecould u answer?
i can't understand why did u put // before . and separate it from xyz
and why it's wrong to write it (".xyz")
This answer is for kotlin users
ReplyDeletefun main() {
print(xyzThere("abc.xyzxyz"))
}
fun xyzThere(str: String): Boolean{
if (str.contains(".xyz")){
return false
}
return (str.contains("xyz"))
}
public boolean xyzThere(String str) {
ReplyDeletefor (int i = 0; i < str.length() - 2; i++) {
if (str.substring(i,i+3).equals("xyz") && (i == 0 || str.charAt(i - 1) != '.')) {
return true;
}
}
return false;
}
public boolean xyzThere(String str) {
ReplyDeletereturn str.replaceAll("(\\.)(xyz)", "").contains("xyz");
}
fails for "x.xyzyx"
Deletepublic boolean xyzThere(String str) {
ReplyDeletefor(int i=0;i<str.length()-2;i++){
if(str.substring(i,i+3).compareTo("xyz")==0 && i==0){
return true;
}
if(str.substring(i,i+3).compareTo("xyz")==0 && str.charAt(i-1)!='.'){
return true;
}
}
return false;
}
public boolean xyzThere(String str) {
ReplyDeleteint count=0;
if(str.length()<3){
return false;
}
for(int i=0;i<str.length()-2;i++){
if(str.charAt(i) == '.') i++;
else if(str.substring(i,i+3).equals("xyz")){
count++;
return true;
}
}return false;
}
** This code is not giving me a 100% output and does not show which testcase is wrong. So what the hell is wrong with it**
ReplyDeletepublic boolean xyzThere(String str) {
if (str.contains(".xyz")) {
String[] as=str.split(".xyz");
for (String s: as){
if (s.contains("xyz")) return true;
}
}
else if (str.contains("xyz")) return true;
return false;
}
public boolean xyzThere(String str) {
ReplyDeleteif(str.length()>2 && str.substring(0,3).equals("xyz")) return true;
for(int i=0;i<str.length()-3;i++){
if(str.charAt(i)!='.' && str.substring(i+1,i+4).equals("xyz")){
return true;
}
}
return false;
}
public boolean xyzThere(String str)
ReplyDelete{
for(int i = str.length(); i > 2; i--)
{
if(str.substring(i-3,i).equals("xyz"))
{
if(str.length() > 3 && i > 3 && str.substring(i-4, i-3).equals(".")) return false;
else return true;
}
}
return false;
}
int withDot = 0;
ReplyDeleteint withOutDot = 0;
for (int i = 0; i < str.length(); i++) {
if (str.startsWith(".xyz", i)) withDot++;
if (str.startsWith("xyz", i)) withOutDot++;
}
return (withOutDot > withDot);
static void func(String str){
ReplyDeletefor(int i=0;i<str.length()-2;i++){
if(str.charAt(i)=='x'){
if(str.substring(i,i+3).equals("xyz")){
if(str.charAt(i-1)=='.'||str.charAt(i+1)=='.'){
System.out.println("false");
}
else{
System.out.println("true");
}
}
}
}
}
not allowed to use sysout
Deleteuse return
shorter and newest way for this code: public boolean xyzThere(String str) {
ReplyDeleteif (str.contains(".xyz")) {
str = str.replace(".xyz", "");
}
return str.contains("xyz");
}
public boolean xyzThere(String str) {
ReplyDeleteif(str.length()==3&&str.substring(0,str.length()).equals("xyz"))return true;
for(int i=0; i<str.length()-3;i++){
if(str.substring(0,3).equals("xyz"))return true;
if(str.charAt(i)!='.'&&str.charAt(i+1)=='x'&&str.charAt(i+2)=='y'&&
str.charAt(i+3)=='z')return true;
}return false;
}