Code Monkey home page Code Monkey logo

java-string-manipulation's Introduction

Overview : Java String Manipulation

Table of Contents

Penjelasan Singkat

countFrequentLetterInsensitive

Pertama, bersihkan text yang diinputkan dari whitespace dan jadikan semua huruf menjadi lowercase.

text = text.replace(" ", "").toLowerCase();

Kemudian, hitung frekuensi tiap huruf yang muncul dengan menggunakan map. map.put() digunakan untuk menyimpan symbol atau huruf dari text yang diberikan. map.getOrDefault() digunakan untuk mendapatkan value pada map berdasarkan key. ketika tidak ada maka akan memberikan nilai default sesuai yang ditentukan

for (int i = 0; i < text.toCharArray().length; i++) {
    char symbol = text.toCharArray()[i];
    frequencyMap.put(symbol, frequencyMap.getOrDefault(symbol, 0) + 1);
}

Lalu, sorting huruf atau symbol sesuai dengan urutan text yang diberikan

for (char c : text.toCharArray()) {
    String temp = c + "=" + frequencyMap.get(c) ;
    if (!list.contains(temp)) {
        list.add(temp);
    }
}

Setelah itu, string yang masih berbentuk list akan konversi menjadi bentuk string sesuai dengan Output

String stringList = list.toString();
System.out.println(stringList.replace("[","\"").replace("]","\""));

Sample

Input:

“We Always Mekar”

Output :

"w=2, e=2, a=3, l=1, y=1, s=1, m=1, k=1, r=1"

Input:

“coding is fun”

Output :

"c=1, o=1, d=1, i=2, n=2, g=1, s=1, f=1, u=1"

countFrequentLetterSensitive

Pertama, konversi list of word menjadi sebuah string

StringBuilder builder = new StringBuilder();
for (String word : words) {
    builder.append(word);
}

Berikutnya, hitung frekuensi setiap huruf yang muncul (Case Sensitive).

Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : builder.toString().toCharArray()) {
    frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
}

Kemudian, urutkan berdasarkan frekuensi (descending)

int freqCompare = b.getValue().compareTo(a.getValue());
if (freqCompare != 0) {
    return freqCompare;
}

Jika frekuensi sama, urutkan huruf besar terlebih dahulu

boolean aIsLower = Character.isLowerCase(a.getKey());
boolean bIsLower = Character.isLowerCase(b.getKey());
if (aIsLower != bIsLower) {
    return aIsLower ? 1 : -1;
}

Jika sama-sama besar atau sama-sama kecil, urutkan berdasarkan abjad

return Character.compare(a.getKey(), b.getKey());

Sample

Input :

Input:
[“Abc”, “bCd”]

Output :

bACcd

Input :

Input:
[“Oke”, “One”]

Output :

Oekn

Input :

Input:
[“Pendanaan”, “Terproteksi”, “Untuk”, “Dampak”, “Berarti”]

Output :

aenrktipBDPTUdmosu

java-string-manipulation's People

Contributors

irfans18 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.