Java > String-2 > xyzThere (CodingBat Solution)

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;
}


40 comments :

  1. Bullshit. This is my code:

    public boolean xyzThere(String str) {
    str = str.replaceAll("(\\.)(xyz)", "");
    return str.contains("xyz");
    }

    Much better, isn't it?

    ReplyDelete
    Replies
    1. 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.

      Students 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.

      Delete
    2. You say bullshit but your solution isn't correct. For example yours returns the wrong answer for "x.xyzyz".

      Delete
    3. ahahahah you are right. that guy...

      i 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;
      }

      Delete
    4. ur literally a legend dude

      Delete
    5. took 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.

      Delete
    6. i need explanation for it
      str = str.replaceAll("(\\.)(xyz)", "");

      Delete
    7. 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.

      Delete
    8. I can not understand this step
      str = str.replaceAll("(\\.)(xyz)", "");

      Delete
    9. yes bro, this is much better and thanks

      Delete
  2. It's always better to use a primitive than an Object, so I recommend you change Boolean to boolean.

    ReplyDelete
  3. Where does the 46 come from?

    ReplyDelete
  4. I used a different method. I present to you the method of brute forceing your code to work.

    public boolean xyzThere(String str) {
    Random ran = new Random();
    x = ran.nextInt(2);

    if(x==0)
    return true;
    return false;
    }

    ReplyDelete
  5. Why this doesn`t work with str like "abcxyz", "abc.xyzxyz", "abc.xxyz", 12xyz ?

    public boolean xyzThere(String str) {

    String xyz = "xyz";
    String strReplace = str.replaceAll(".xyz","");

    if (strReplace.contains(xyz)){
    return true;
    } else {
    return false;
    }

    }

    ReplyDelete
    Replies
    1. public boolean xyzThere(String str) {
      String xyz = "xyz";
      String strReplace = str.replaceAll(".xyz","");
      if (strReplace.contains(xyz)){
      return true;
      } else {
      return false;
      }
      //here.. missing return statment
      }

      Delete
  6. public boolean xyzThere(String str) {
    int 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

    ReplyDelete
    Replies
    1. I don't understand what kind of loop this is and what exactly it does?

      " for (int i=0; ia;} "

      Can somebody please elaborate it for me?

      Delete
    2. @gaman: Seems to be broken and missing code.

      Delete
  7. Try this

    public 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;
    }

    ReplyDelete
  8. public boolean xyzThere(String str)
    {
    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;
    }

    ReplyDelete
  9. yehh.
    could 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")

    ReplyDelete
    Replies

    1. could 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")

      Delete
  10. This answer is for kotlin users
    fun main() {
    print(xyzThere("abc.xyzxyz"))
    }

    fun xyzThere(str: String): Boolean{
    if (str.contains(".xyz")){
    return false
    }
    return (str.contains("xyz"))
    }

    ReplyDelete
  11. public boolean xyzThere(String str) {
    for (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;
    }

    ReplyDelete
  12. public boolean xyzThere(String str) {
    return str.replaceAll("(\\.)(xyz)", "").contains("xyz");
    }

    ReplyDelete
  13. public boolean xyzThere(String str) {
    for(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;
    }

    ReplyDelete
  14. public boolean xyzThere(String str) {
    int 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;
    }

    ReplyDelete
  15. ** 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**

    public 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;
    }

    ReplyDelete
  16. public boolean xyzThere(String str) {
    if(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;
    }

    ReplyDelete
  17. public boolean xyzThere(String str)
    {
    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;
    }

    ReplyDelete
  18. int withDot = 0;
    int 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);

    ReplyDelete
  19. static void func(String str){
    for(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");
    }
    }
    }
    }

    }

    ReplyDelete
  20. shorter and newest way for this code: public boolean xyzThere(String str) {
    if (str.contains(".xyz")) {
    str = str.replace(".xyz", "");
    }
    return str.contains("xyz");
    }

    ReplyDelete
  21. public boolean xyzThere(String str) {
    if(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;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact