Heim > Java > javaLernprogramm > Hauptteil

Versuchen Sie es

王林
Freigeben: 2024-07-29 10:20:12
Original
709 Leute haben es durchsucht

Versuchen Sie es

Implementierung der Versuchen Sie es-Datenstruktur

Striver-Erklärung der Versuchen Sie es-Datenstruktur

class Node{
    Node [] node = new Node[26];
    boolean flag;
    public Node(){

    }
    public boolean containsKey(char c){
        return node[c-'a']!=null;
    }
    public void put(char c, Node n){
        node[c-'a']  = n;
    }
    public Node get(char c){
        return node[c-'a'];
    }
    public void setFlag() {
        this.flag = true;
    }
    public boolean getFlag(){
        return this.flag;
    }
}

class Versuchen Sie es {
    Node root;
    public Versuchen Sie es() {
        root = new Node();
    }
    //will take tc : O(len) of the word
    public void insert(String word) {
        Node node  = root;
        for(int i =0;i<word.length if node.put node node.setflag take tc : o of the word public boolean search for i="0;i<word.length();i++){" return false node.getflag prefix startswith true your versuchen sie es object will be instantiated and called as such: obj="new" obj.insert param_2="obj.search(word);" param_3="obj.startsWith(prefix);">



<h2>
  
  
  Versuchen Sie es mit der Datenstruktur II
</h2>

<p>Streber-Erklärung zum besseren Verständnis<br>
</p>

<pre class="brush:php;toolbar:false">import java.util.* ;
import java.io.*; 

class Node {
    Node node[] = new Node[26];
    int endWith = 0;// will keep track of no. of words ending with this word
    int countPrefix=0;// will keep track of no. of words starting with this word
    public Node(){

    }
    public boolean containsKey(char c){
        return node[c-'a']!=null;
    }
    public void put(char c, Node n){
        node[c-'a'] = n;
    }
    public Node get(char c){
        return node[c-'a'];
    }
    public void incrementCountPrefix() {
        ++this.countPrefix;
    }
    public void decrementCountPrefix(){
        --this.countPrefix;
    }
    public void incrementEndWith(){
        ++this.endWith;
    }
    public void deleteEndWith(){
        --this.endWith;
    }
    public int getCountPrefix(){
        return this.countPrefix;
    }
    public int getEndWith(){
        return this.endWith;
    }


}
public class Versuchen Sie es {
    Node root;
    public Versuchen Sie es() {
        // Write your code here.
        root = new Node();
    }

    public void insert(String word) {
        Node node = root;
        for(int i =0;i<word.length if node.put node node.incrementcountprefix node.incrementendwith public int countwordsequalto word write your code here. for i="0;i<word.length();i++){" else return node.getendwith will tell how many strings are with given countwordsstartingwith node.getcountprefix it starting void erase node.decrementcountprefix node.deleteendwith>



<h2>
  
  
  Komplette Zeichenfolge
</h2>



<pre class="brush:php;toolbar:false">// tc : O(n*l)

import java.util.* ;
import java.io.*; 

class Node{
    Node[] node = new Node[26];
    boolean flag;
    public Node(){}
    public void put(char c , Node n){
        node[c-'a'] = n;
    }
    public boolean containsKey(char c){
        return node[c-'a']!=null;
    }
    public Node get(char c){
        return node[c-'a'];
    }
    public void setEnd(){
        this.flag = true;
    }
    public boolean isEnd(){
        return this.flag;
    }
}

class Versuchen Sie es{
    Node root;
    public Versuchen Sie es(){
        root = new Node();
    }
    public boolean checkIfPrefixPresent(String s){
      Node node = root;
      boolean flag= true;
      for(int i = 0;i<s.length char c="s.charAt(i);" if return false node="node.get(c);" flag="flag" node.isend this will check the substring is also a string from list of strings line work here because any not present as in trie then s won be complete and we can only public void insert for i="0;i<s.length();i++){" node.put new node.setend setting end current class solution static root completestring n versuchen sie es all data structure : trie.insert out comeplete among s.length selection a.compareto b lexographically else s.compareto completestring.equals>



<h2>
  
  
  Zählen Sie eindeutige Teilzeichenfolgen
</h2>

<p>Tc: O(n^2) zum Einfügen verschiedener eindeutiger Teilzeichenfolgen in <br>
Versuchen Sie es mit der Datenstruktur<br>
</p>

<pre class="brush:php;toolbar:false">
import java.util.ArrayList;

public class Solution 
{
    Node root;
    static int count;
    public Solution(){
        root = new Node();
    }

    public static int countDistinctSubstrings(String s) 
    {
        count = 0;
        //    Write your code here.
        Solution sol = new Solution();
        for(int i =0;i




          

            
  

            
        
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonVersuchen Sie es. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!