This problem requires counting occurrences of valid words in a concatenated string, where a valid word consists of lowercase English letters and hyphens only when surrounded by letters on both sides, with any other character acting as a separator. The solution involves concatenating all chunks into a single string, then iterating through it to identify valid words by checking character types and hyphen validity, storing frequencies in a map, and returning query results.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Count Valid Word Occurrences | LeetCode 3926 | Weekly Contest 501 | Java | Developer CoderAdded:
[music] >> Hello everyone, welcome back to my YouTube channel. So now I'm going to solve question number two of weekly contest 501.
Count valid word occurrences. So in this problem you are given an array of strings chunks.
The strings are concatenated in order to form a single string S.
You are also given an array of strings queries.
A word is defined as a substring of S that consists of lower case English letters A to Z.
May include hyphens only if each hyphen is surrounded by lower case English letters.
Each hyphen should be surrounded by lower case English letters.
And is not part of a longer substring that also satisfies the above conditions.
Then a word is defined as a substring of S.
Any character that is not a lower case English letter or a valid hyphen act as a separator.
We have to return an integer array answer such that answer of I is the number of occurrences of queries of I's as a word in S.
A substring is a contiguous non-empty sequence of characters within a string.
Okay.
So let's see what we need to do here. So see this example number one. So chunks is like this, right? So first merge them, right? Concatenate them. So it is going to look like this. Hello world hello, something like this. Okay.
So you have to create one string builder.
And in string builder you have to add all these characters one by one. Append all the characters. Okay.
So you will get something like this.
Hello world hello.
Concatenating all strings in chunks gives you this, right?
Then here hello hello is appearing two times.
Done.
World is appearing one time.
w o r, it is not appearing anywhere, right? So, it's zero. That's it.
The valid words in S are hello which appears twice and world which appears once.
Thus, the answer is this. Okay, we will see a few more examples, all right?
So, here you have something like this.
So, first concatenate everything. So, you will get something like this.
A, then two hyphens are there, okay?
Then, A, then hyphen, then again hyphen, then C, all right?
Now, here start from here. A, then after that hyphen is there, okay? So, now here before this hyphen there is one lowercase English character, but after this hyphen there is one more hyphen.
So, that simply means that this is not a valid word, right? So, as of now, we have just this word as of now. So, just add this A. A frequency is one as of now, okay?
Then after that, move further.
So, here you see one more hyphen, right?
I'm just iterating over all the characters in this particular concatenated string.
Okay, there is one hyphen, but before that there is more one more hyphen, right? So, this is not a valid starting point. Right? And a valid starting point is always going to start from a lowercase English character. Right? Then after that, B.
Okay, B is there. Then after that, there is one space, right? This is a as a separator. So, now B frequency is one, right?
Now after that, you move here. A, okay?
Then after that, there is one hyphen, okay? So, I will check if before that there is one lowercase English character and after that also there should be one lowercase English character. No, it is not there. So, break it. So, A A is appearing now two times.
Then after that, I move here. This is not a valid starting point. Then I move here.
Okay? So, here I see C.
And that's it. There's nothing after that. So, in the end we are left with just this particular word C. C is occurring one time.
So, now it is saying queries is A. A is occurring two times. That's it. B is occurring one time. That's it. C is occurring one time. That's it.
So, this is what you need to do.
Concatenating all strings in chunks gives you this.
The valid words in S are A which appears twice, B which appears once, and C which appears once. So, the answer is this.
That's it.
So, that's what we need to do. Very simple. Here if you see this example number three.
Chunks is like this. Only hello we have, right? So, it is asking us to search for hello. Hello is appearing appearing one time.
L is not appearing. So, that's it. 1 0.
The valid word in S is hello which appears once. So, the answer is this.
That's it. And here if you see the constraints. So, chunks.length is from 1 to 10^5. Chunks i.length is from 1 to 10^5.
Chunks of i may consist of lower case English letters.
Spaces and hyphens. Okay. So, chunks of i only consist of lower case English letters.
Spaces and hyphens. That's it. We will not see anything else.
All right. That's a good thing actually, right? The total height total length of all the strings in chunks doesn't exceed this. Okay.
Queries.length is from 1 to 10^5. Okay.
Queries i.length is from 1 to 10^5.
Queries of i is a valid word. All right.
So, this is going to be one valid word only, right?
>> [snorts] >> All right.
The total length of all the strings in queries doesn't exceed this.
Input. Okay. So, here concatenate everything first. A hyphen B. C hyphen D. It's already one only, right? So, concatenate it only, right? Now, here if you see A then after it hyphen is there.
And here if you see lowercase, here you see lowercase. Then move further, B.
Okay, then there is one separator. So, this is one valid word. A-B is one valid word. Okay. Now, after that proceed here.
Uh space, then C start from C, then hyphen. That's it. A-B occurs one time.
Here lowercase, not lowercase, so C is one word, right?
Then this is not a starting word, then D, then after that there is one separator, so D is one valid word. Then here E E, okay.
You can start from E, then hyphen. So, this is valid. F is valid.
Uh here hyphen is valid, then G, then there is end. So, we have to add this EFG. This is also valid. Right? So, now here if you see, uh these are the queries. So, E is occurring zero time.
You are not seeing A here. B is not here. AB is not here. CD is not here. C is here one time. D is here one time.
EFG is not here, right? Okay, that's it.
That's it. So, now let's see the code.
So, first thing is nothing but concatenation you need to do.
So, string builder you are getting and then you are iterating over all these chunks one by one.
And you are just concatenating all the strings. That's it. Then once you concatenate all the strings to string builder, just convert it to one string, big string.
Right? Then after that you have to extract the valid words.
So, you are creating one map, string and integer frequency, and you are going to create one string builder word. And N is nothing but the S.length, right? And then you are iterating over all the N elements you have.
Over all the N characters, right? Of this string.
This particular string you are iterating over here.
Then you are going to pick that particular character.
And then you have to check if the character is lowercase, then just append this to your string builder.
If it is a hyphen, then you have to check it is a valid hyphen only if surrounded by letters. So, if word.length is greater than zero, right? And the next I plus one, it should be not go out of bound, right? And character is a lowercase at s.
character at I plus one, okay? If character is a lowercase at this particular position, right? Then only you have to continue forming the word. Otherwise, you have to add the word in the in this frequency map, okay? So, here frequency you're passing and the particular word, okay? So, this is the one method I will write.
If it is separator, then also you have to add the word, right? And last word also we need to add, right? In the end, the last word. Because after last word, you will not see any separator or anything, right? So, you have to add that as well.
So, after that, you have to iterate over the queries and you have to just take the frequency and return that.
That's it. This is what you need to do here.
Clear?
Now, how this add word is going to work?
So, simple here, you are passing the map and the word. If word length is zero, just simply return. There's nothing to do.
Otherwise, just change this particular string builder to string.
Then you have to remove the invalid trailing hyphens, right?
So, if you see something like this, then you have to remove it, right? If it is there. You can ignore this line as well.
I don't think it will create any issue.
You have to check if the word is becoming empty or not. So, if it is not empty, then you have to operate this to frequency, right?
And once all this is done, then you have to set the length of this particular word to zero, right? Because you have to move to the next word.
So, that's what we need to do, right?
So, I hope that you guys are able to understand question number two of weekly contest 501.
And if you guys still have any doubts, then please feel free to ask in comment section.
Till then, keep coding and keep practicing. We'll see you in the next video. Thank you. Bye-bye.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsโข2026-05-28
How agent o11y differs from traditional o11y โ Phil Hetzel, Braintrust
aiDotEngineer
450 viewsโข2026-05-28
Re: ๐ฃ๏ธ๐theprophedu๐2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 viewsโข2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation๐ฏโ
LearnwithSahera
1K viewsโข2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsโข2026-05-29
Search Algorithms Explained in 60 Seconds! ๐ค๐จ
samarthtuliofficial
218 viewsโข2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsโข2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsโข2026-05-29











