check Str's Character appearence frequence is ge 1 美国加利福尼亚州某所大学的算法测试题

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.*;

public class HackerRankTest {

    public static void main(String[] args) {
        String srcStr="We promptly judged antique ivory buckles for the next prize";
        String srcStr1="We promptly judged antique ivory buckles for the prize";
        boolean checkStrIsSubStr = checkStrIsSubStr(srcStr);
        if (checkStrIsSubStr){
            System.out.println("pangram");
        }else {
            System.out.println("not pangram");
        }
        boolean checkStrIsSubStr1 = checkStrIsSubStr(srcStr1);
        if (checkStrIsSubStr1){
            System.out.println("pangram");
        }else {
            System.out.println("not pangram");
        }
    }

    /**
     * judge Str's Character appearence frequence is ge 1.
     * @param srcStr
     * @return
     */
    public static boolean checkStrIsSubStr(String srcStr){
        if (srcStr==null){
            return false;
        }
        if (srcStr.isEmpty()){
            return false;
        }
        int length = srcStr.length();
        if (length<1 || length>1000){
            return false;
        }
        for (int i = 0; i < length; i++) {
            char charAt = srcStr.charAt(i);
            if (charAt==' '){
                continue;
            }else if (charAt>='A' && charAt<= 'z'){
                continue;
            }else {
                return false;
            }
        }
        HashMap<Character, Integer> characterIntegerHashMap = new HashMap<Character, Integer>();
        String srcStrDest = srcStr.replaceAll("\\s", "&");
        for (int i = 0; i < length; i++) {
            char charAt = srcStrDest.charAt(i);
            if (characterIntegerHashMap.containsKey(charAt)){
                characterIntegerHashMap.put(charAt,characterIntegerHashMap.get(charAt)+1);
            }else {
                characterIntegerHashMap.put(charAt,1);
            }
        }
        ArrayList<CustomerCharacterRec> customerCharacterRecArrayList = new ArrayList<CustomerCharacterRec>();
        for (Map.Entry<Character,Integer> entry: characterIntegerHashMap.entrySet())  {
            Character key = entry.getKey();
            Integer entryValue = entry.getValue();
            CustomerCharacterRec customerCharacterRec = new CustomerCharacterRec();
            UUID randomUUID = UUID.randomUUID();
            String toString = randomUUID.toString();
            customerCharacterRec.setId(toString);
            customerCharacterRec.setSrcCharacter(key);
            customerCharacterRec.setCountFrequence(entryValue);
            customerCharacterRecArrayList.add(customerCharacterRec);
        }
        System.out.println(customerCharacterRecArrayList);
        String srcStrStr=" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        HashMap<Character, Integer> characterIntegerHashMap1Src = new HashMap<Character, Integer>();
        for (int i = 0; i < srcStrStr.length(); i++) {
            char charAt = srcStrStr.charAt(i);
            if (charAt==' '){
                charAt='&';
            }
            characterIntegerHashMap1Src.put(charAt,1);
        }
        ArrayList<CustomerCharacterRec> customerCharacterRecs1SrcList = new ArrayList<CustomerCharacterRec>();
        for (Map.Entry<Character,Integer> entry : characterIntegerHashMap1Src.entrySet()) {
            Character key = entry.getKey();
            Integer entryValue = entry.getValue();
            CustomerCharacterRec customerCharacterRecSrc = new CustomerCharacterRec();
            UUID randomUUID = UUID.randomUUID();
            String toString = randomUUID.toString();
            customerCharacterRecSrc.setId(toString);
            customerCharacterRecSrc.setSrcCharacter(key);
            customerCharacterRecSrc.setCountFrequence(entryValue);
            customerCharacterRecs1SrcList.add(customerCharacterRecSrc);
        }
        System.out.println("---------srcDataSet------");
//        System.out.println(customerCharacterRecs1SrcList);
        Collections.sort(customerCharacterRecs1SrcList,new Comparator<CustomerCharacterRec>() {
            @Override
            public int compare(CustomerCharacterRec o1, CustomerCharacterRec o2) {
                if (o1.getSrcCharacter()>o2.getSrcCharacter()){
                    return 1;
                }else if(o1.getSrcCharacter()<o2.getSrcCharacter()){
                    return -1;
                }
                return 0;
            }
        });
        System.out.println(customerCharacterRecs1SrcList);
        Collections.sort(customerCharacterRecArrayList, new Comparator<CustomerCharacterRec>() {
            @Override
            public int compare(CustomerCharacterRec o1, CustomerCharacterRec o2) {
                if (o1.getSrcCharacter()>o2.getSrcCharacter()){
                    return 1;
                }else if (o1.getSrcCharacter()<o2.getSrcCharacter()){
                    return -1;
                }
                return 0;
            }
        });
        System.out.println(customerCharacterRecArrayList);
        for (CustomerCharacterRec customerCharacterRec : customerCharacterRecs1SrcList) {
            Character srcCharacter = customerCharacterRec.getSrcCharacter();
            Integer countFrequence = customerCharacterRec.getCountFrequence();
            for (CustomerCharacterRec characterRec : customerCharacterRecArrayList) {
                Character srcCharacter1 = characterRec.getSrcCharacter();
                Integer countFrequence1 = characterRec.getCountFrequence();
                if (srcCharacter==srcCharacter1){
                    if (countFrequence1>=countFrequence){
                        return true;
                    }else {
                        return false;
                    }
                }
            }
        }
        return false;
    }

}

class CustomerCharacterRec{
    private String id;
    private Character srcCharacter;
    private Integer countFrequence;
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Character getSrcCharacter() {
        return srcCharacter;
    }

    public void setSrcCharacter(Character srcCharacter) {
        this.srcCharacter = srcCharacter;
    }

    public Integer getCountFrequence() {
        return countFrequence;
    }

    public void setCountFrequence(Integer countFrequence) {
        this.countFrequence = countFrequence;
    }

    @Override
    public String toString() {
        return "CustomerCharacterRec{" +
                "id='" + id + '\'' +
                ", srcCharacter=" + srcCharacter +
                ", countFrequence=" + countFrequence +
                '}';
    }
}

  

posted @ 2024-08-10 16:28  coderlwz  阅读(15)  评论(0编辑  收藏  举报