Detecting if a string has unique characters: comparing my solution to “Cracking the Coding Interview?”

后端 未结 7 1836
孤街浪徒
孤街浪徒 2020-12-22 16:43

I am working through the book \"Cracking the Coding Interview\" and I have come across questions here asking for answers, but I need help comparing my answer to the solution

相关标签:
7条回答
  • 2020-12-22 17:46

    As referenced from 'Cracking the Coding Interview', an alternative solution exists:

    boolean isUniqueChars(String str) {
      if(str.length() > 128) return false;
    
      boolean[] char_set = new boolean[128];
      for(int i = 0; i < str.length(); i++) {
        int val = str.charAt(i);
    
        if(char_set[val]) {
          return false;
        }
        char_set[val] = true;
      }
      return true;
    }
    

    Of course, to achieve better space complexity, please refer to the above example by @templatetypedef

    0 讨论(0)
提交回复
热议问题