[Algorithm question] Interview question 01.01. Determine whether a character is unique

Source of topic: "Programmer Interview Classic (6th Edition)"

1. Problem: Interview question 01.01. Determine whether the character is unique

Interview question 01.01. Determine whether a character is unique

1. Topic description

insert image description here

Two, the solution

(1) Option 1

1. Problem-solving ideas
  1. The first round of traversal: starting from the first character, it is compared with all subsequent characters in turn. If there is the same, directly return false and exit the loop; if there is no same, continue to the second round of traversal.
  2. The second round of traversal: starting from the second character, it is compared with all subsequent characters in turn.
  3. The third round of traversal: starting from the third character, it is compared with all subsequent characters in turn.
  4. And so on, until the n-1th round of traversal is completed.
2. Problem-solving method

Use double loops for ergodic solving.

3. Complexity
  • Time complexity: O ( n 2 ) O(n^2)O ( n2)

  • Space Complexity: O ( n ) O(n)O ( n )

4. Code implementation
class Solution {
    
    
    public boolean isUnique(String astr) {
    
    
        for(int i=0; i<astr.length()-1; i++) {
    
    
            char ch = astr.charAt(i);
            for(int j=i+1; j<astr.length(); j++) {
    
    
                if(ch == astr.charAt(j)) {
    
    
                    return false;
                }
            }
        }
        return true;
    }
}

(2) Option 2

1. Idea

Use HashMap to store characters, and count the number of characters. When the number of characters is greater than 1, it means that there are repeated characters, and return false directly. When all the characters in the string are traversed and there are still no repeated characters, it returns true.

2. Problem-solving method

Use HashMap to store characters and count the number of characters.

3. Complexity
  • Time complexity: O ( n ) O(n)O ( n )

  • Space Complexity: O ( n ) O(n)O ( n )

4、Code
class Solution {
    
    
    public boolean isUnique(String astr) {
    
    
        Map<Character, Integer> map = new HashMap();
        for(int i=0; i<astr.length(); i++) {
    
    
            char ch = astr.charAt(i);
            if(map.get(ch) == null || map.get(ch) == 0) {
    
    
                map.put(ch, 1);
            } else {
    
    
                map.put(ch, map.get(ch)+1);
            }
            if(map.get(ch) > 1) {
    
    
                return false;
            }
        }
        return true;
    }
}

(3) Option 3

1. Idea

Constructs an array of Boolean values, the flag corresponding to the index value i indicates whether the string contains the ith character of the alphabet.

2. Problem-solving method

Array of boolean values.

3. Complexity
  • Time complexity: O ( n ) O(n)O ( n )

  • Space Complexity: O ( 1 ) O(1)O(1)

4、Code

/**
 * 构造一个布尔值的数组,索引值 i 对应的标记指示该字符串是否含有字符表第 i 个字符。
 */
class Solution {
    
    
    public boolean isUnique(String astr) {
    
    
        if(astr.length() > 128) {
    
     // 如果字符串的长度超过了字母表中不同字符的个数,则直接返回false
            return false;
        }

        boolean[] char_set = new boolean[128];
        for(int i=0; i<astr.length(); i++) {
    
    
            int val = astr.charAt(i); 
            if(char_set[val]) {
    
     // 在字符串中已找到该字符
                return false;
            }
            char_set[val] = true;
        }
        return true;
    }
}

3. Test verification

Option One:
insert image description here

Option II:
insert image description here

third solution:insert image description here

Guess you like

Origin blog.csdn.net/Shipley_Leo/article/details/131140880