striverstrieseries's People
Forkers
ashishr1234 developerdiver zhcet19 sujankumarmitra sohamsen93 gauravghatge singhvarun08 simar05519 ishita-kundi04 stuti0901 akshay0101 tarunsharma000 dhrubajyotinath6 i-am-atif devendr09 sumendra2906 baibhavdatta21 surbhi498 amitbhardwaj26 rajarshisg shawavisek35 nk8713694 ashishsharma1998 abinasherz939 vishal11912056 swetank-367 pal-03 tejas59 shoolapani kishan779 karankc23 narenm1234 paliwalumesh bansal1999 akakash55 abhishekchauhan9036 vartika2302 aakashguitarist jash459 abhisheksingh10022 anuj2801 satwikcoolagrawal hardikmaind kinzs2001 sarthakgautam adhiraj-12 roshan-jha-23striverstrieseries's Issues
L4_DistinctSubstrings_Cpp
I have a similar solution and tried copy-pasting your solution on the coding ninjas platform. I am getting TLE and my score is 88. How can we optimize this?
My code: https://pastebin.com/XRHhLaw9
C++ code is added instead of the Java code in StriversTrieSeries/L3_Complete_String_Java
Please add the Java code.
//Updated : Here is the working java code for this problem -
class Node{
Node links[] = new Node[26];
boolean flag = false;
public Node(){
}
boolean containsKey(char ch)
{
return (links[ch -'a'] != null);
}
void put(char ch, Node node)
{
links[ch-'a'] = node;
}
Node get(char ch)
{
return (links[ch-'a']); // ch -'a' gives corresponding ascii of the particular alphabet
}
void setEnd()
{
flag = true;
}
boolean isEnd()
{
return flag;
}
};
class Trie{
private static Node root;
//whenever we require object of this trie we know we'll be needing a new trie
Trie(){
root = new Node();
}
public static void insert(String word)
{
Node node = root;
for(int i = 0;i<word.length();i++)
{
if(!node.containsKey(word.charAt(i))) {
node.put(word.charAt(i), new Node());
}
node = node.get(word.charAt(i));
}
node.setEnd();
}
boolean checkIfAllPrefixExists(String word)
{
Node node = root;
boolean flag = true;
for(int i = 0;i<word.length() && flag;i++) {
if(node.containsKey(word.charAt(i))) {
node = node.get(word.charAt(i));
flag = flag & node.isEnd();
}
else {
return false;
}
}
return flag;
}
}
class Solution {
public static String completeString(int n, String[] a) {
Trie obj = new Trie();
for(int i =0; i<n; i++)
{
obj.insert(a[i]);
}
String longest = "";
for(int i= 0; i<n; i++)
{
if(obj.checkIfAllPrefixExists(a[i]))
{
if(a[i].length() > longest.length())
{
longest = a[i];
}
else if(a[i].length() == longest.length() && a[i].compareTo(longest) < 0)
{
longest = a[i];
}
}
}
if(longest == "") return "None";
return longest;
}
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.