Leetcode LeetCode algorithm punch card learning day 8

Three, the first unique character in the string

Given a string, find its first unique character and return its index. If it does not exist, it returns -1.
Insert picture description here
1. Mine-use hash table statistics

class Solution {
    
    
    public int firstUniqChar(String s) {
    
    
        Map<String, Integer> hashtable = new HashMap<String, Integer>();
        //用哈希表统计字符串出现次数
        for (int i = 0; i < s.length(); i++) {
    
    
            String x=s.substring(i,i+1);
            if (hashtable.containsKey(x)) {
    
    
                hashtable.put(x,hashtable.get(x)+1);
            }
            else{
    
    
                hashtable.put(x,1);
            }
        }
        //再遍历字符串查看其在哈希表中存储的字数
        for(int j=0;j<s.length();j++){
    
    
            String x=s.substring(j,j+1);
            if(hashtable.get(x)==1){
    
    
                return j;
            }
        }
        return -1;
    }
}

2. Officially, it is also a hash table

class Solution {
    
    
    public int firstUniqChar(String s) {
    
    
        Map<Character, Integer> frequency = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); ++i) {
    
    
            char ch = s.charAt(i);
            frequency.put(ch, frequency.getOrDefault(ch, 0) + 1);
        }
        for (int i = 0; i < s.length(); ++i) {
    
    
            if (frequency.get(s.charAt(i)) == 1) {
    
    
                return i;
            }
        }
        return -1;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/first-unique-character-in-a-string/solution/zi-fu-chuan-zhong-de-di-yi-ge-wei-yi-zi-x9rok/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Four, effective letter dysphorisms

Given two strings s and t, write a function to determine whether t is an anagram of s.
Insert picture description here
Method 1: Mine. Use a hash table for statistics.

class Solution {
    
    
    public boolean isAnagram(String s, String t) {
    
    
        if(s.length()!=t.length()){
    
    
            return false;
        }
        //为第一个字符串创建哈希表
        Map<String,Integer> hashset1=new HashMap<String,Integer>();
        //为第二个字符串创建哈希表
        Map<String,Integer> hashset2=new HashMap<String,Integer>();
        
        for(int i=0;i<s.length();i++){
    
    
            String x=s.substring(i,i+1);
            String y=t.substring(i,i+1);
            //统计字符串s的哈希表
            if (hashset1.containsKey(x)) {
    
    
                hashset1.put(x,hashset1.get(x)+1);
            }
            else{
    
    
                hashset1.put(x,1);
            }
            //统计字符串t的哈希表
            if (hashset2.containsKey(y)) {
    
    
                hashset2.put(y,hashset2.get(y)+1);
            }
            else{
    
    
                hashset2.put(y,1);
            }
        }
        System.out.println(s.length());
        for(int m=0;m<s.length();m++){
    
    
            String zyzy=s.substring(m,m+1);
            int zy1=hashset1.getOrDefault(zyzy, 0);
            int zy2=hashset2.getOrDefault(zyzy, 0);
            
            if(zy2==0 || zy1 != zy2){
    
    
                return false;
            }
        }
        return true;

    }
}

Method 2: Sort it and see if the strings are equal.

class Solution {
    
    
    public boolean isAnagram(String s, String t) {
    
    
        if (s.length()!=t.length()){
    
    
            return false;
        }
        //排序
        char[] array1 = s.toCharArray();
        char[] array2 = t.toCharArray();
        Arrays.sort(array1);
        Arrays.sort(array2);
        return Arrays.equals(array1,array2);
    }
}

Guess you like

Origin blog.csdn.net/Shadownow/article/details/114259133