首页 > 其他分享 >check Str's Character appearence frequence is ge 1

check Str's Character appearence frequence is ge 1

时间:2024-08-10 16:27:01浏览次数:18  
标签:return String Character frequence ge srcStr CustomerCharacterRec public charAt

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

  

标签:return,String,Character,frequence,ge,srcStr,CustomerCharacterRec,public,charAt
From: https://www.cnblogs.com/liaowanzhong/p/18352435

相关文章

  • 谷歌终于赢了OpenAI一回:实验版本Gemini 1.5 Pro超越GPT-4o
    近两日,谷歌在不断发布最新研究。继昨日放出最强端侧Gemma22B小模型后,刚刚,Gemini1.5Pro实验版本(0801)已经推出。用户可以通过GoogleAIStudio和GeminiAPI进行测试和反馈。既然免费,那我们帮大家测试一下最近比较火的比大小问题。当我们问Gemini1.5Pro(0801......
  • Qt自定义TreeWidget,实现展开折叠按钮在右侧,且一条竖直线上对齐
    效果如下:图片随便找的,可能需要调下样式,代码复制可用,留给有需要的人。 #ifndefCustomTreeWidget_h__#defineCustomTreeWidget_h__#include<QTreeWidget>#include<QPushButton>classCCustomTreeWidget:publicQTreeWidget{ Q_OBJECTpublic: CCustomTreeW......
  • 基于AI的图像生成技术:商汤科技的SenseMirage平台
    摘要:本文介绍了商汤科技推出的AI内容创作社区平台SenseMirage,该平台利用自研的AIGC大模型,实现了快速、高质量的图像生成技术。**关键词:**人工智能,图像生成,内容创作,风格模型1.引言随着人工智能技术的不断进步,AI在艺术创作领域的应用日益广泛。商汤科技作为AI领域的先行者,......
  • CF908D New Year and Arbitrary Arrangement 题解
    Description给定\(k,pa,pb\),有一初始为空的序列。每次有\(\dfrac{pa}{pa+pb}\)的概率往序列后面加一个a。每次有\(\dfrac{pb}{pa+pb}\)的概率往序列后面加一个b。当出现大于等于\(k\)个形如ab的子序列(a和b不一定相邻)时停止。求序列最终的ab子序列期望数。So......
  • 大小核溯源:parallelism 和 heterogeneity
    ETHComputerArchitectureFall2023[1]课程笔记从parallelism到heterogeneityParallelism加速的本质来自Admal'sLaw和Polloack'sRule。理想N核体系相比单核加速比遵守\(s=\frac{1}{p+\frac{1-p}{N}}\),但一味增加并行计算能力不仅边缘递减反而还会degradati......
  • 基于腾讯云EdgeOne的自适应webp转换
    基于腾讯云EdgeOne的自适应webp转换EdgeOne是腾讯云提出的边缘计算相关产品,和CloudFlare的小黄云类似,实际上大致等于CDN+安全防护,同时它还提供了边缘函数来提供我们使用,我们可以借助边缘函数实现一些功能,比如说自动将请求的png或jpg图片转换成webp,从而节省流量,加快网站的访问速度......
  • Integer 包装类day12
    packagecom.shujia.day12;/*Integer的成员方法:publicintintValue()将包装的基本数据类型值取出来publicstaticintparseInt(Strings)String->intpublicstaticStringtoString(inti)int->Stringpublic......
  • Character包装类
    packagecom.shujia.day12;/*Character:是基本数据类型char的包装类成员方法:publicstaticbooleanisUpperCase(charch)判断是否为大写publicstaticbooleanisLowerCase(charch)判断是否为小写publicstaticbooleanisDigit(cha......
  • 《Advanced RAG》-10-Corrective Retrieval Augmented Generation (CRAG)
    摘要CRAG设计了一个轻量级检索评估器,用于评估针对特定查询检索到的文档的整体质量,并使用网络搜索作为改进检索结果的辅助工具。CRAG可与基于RAG的各种方法无缝集成,并提供了一个插件式的解决方案。CRAG的主要思想是引入一个检索评估器,用于评估检索文档与查询之间的关......