/    Sign up×
Community /Pin to ProfileBookmark

Sherlock and Valid String hackerank challenge

A “valid” string is a string S such that for all distinct characters in S each such character occurs the same number of times in S. For example, aabb is a valid string because the frequency of both characters a and b is 2, whereas aabbc is not a valid string because the frequency of characters a, b, and c is not the same. Watson gives a string S to Sherlock and asks him to remove some characters from the string such that the new string is a “valid” string. Sherlock wants to know from you if it’s possible to be done with less than or equal to one removal..

I tried to solve this specific challenge in Hackerank and I can’t pass all the test cases. 2 is the minimum number of removals required to make it a valid string. It can be done in following two ways:

input = aabbcd…

Sample Output must be NO.

Explanation

Remove c and d to get aabb. Or remove a and b to get abcd.

—————————————————–here is my code—————————————————-

[CODE]function processData(input) {
//Enter your code here

var i =0;
var m = string.match(/string[i]/);

if (m && m.length > 2) {
console.log(“yes”);
} else {console.log (“NO”);}

console.log(m);[/CODE]


————————————————————–

The thing is that match() returns null…..
Please if you find time answer my question!
Thank you so much

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 26.2017 — Where did you or how have you arrived at [B]var m = string.match(/string[i]/);[/B] as a pattern?



The match with a proper pattern to match will return an array.
Copy linkTweet thisAlerts:
@FotisauthorJan 26.2017 — Hi!

Please help me by guyding me in the solution.

I don't know what else should I do.

Thank you for taking time reading my question!
Copy linkTweet thisAlerts:
@rootJan 26.2017 — Not sure myself with Regular Expressions as I hardly use them. I can only suggest what I have already pointed out.

The pattern you have /string[i]/ is literally looking for string[i] in your test string



Regular Expressions and Match returns an array of matches, the pattern between the / / 's would be what you are looking to match and if you are looking at /aa|bb|cc|dd/ as the pattern would match aa, bb or cc found in the string you are matching.



This might give you an idea http://docstore.mik.ua/orelly/webprog/jscript/ch10_01.htm



Only way I can think of to find your answer woulf be ti use the output of match() to then run through and replace the matched pairs with a zero length string so you are then left in the original test string with any characters that are not matched.



so...
str = "aabbbcdd"
x = str.match(/aa|bb|cc|dd/ig);
console.log("> "+str);
for(r=0; r<x.length;r++){
console.log(">>> "+x[r])
str = str.replace(x[r],"");
}
// unmatched chars in str
console.log("> "+str);


will result in bc in the console.log which will be unmatched characters that are not pairs.
Copy linkTweet thisAlerts:
@FotisauthorJan 26.2017 — Hi man!

This is my question....?

How to match random values in the match () function,or how to import them.Because still canot import..

[CODE]var x = str.match(/|str[i]|/g);[/CODE]
Copy linkTweet thisAlerts:
@rootJan 26.2017 — If you have something specific then you use the RegExp() object to create the pattern. <i>
</i>var str = "aabccd";
var patt = new RegExp("aa|bb|cc|dd","ig");
var res = str.match( patt );


as for the str[i] like the previous example, you have yet to answer what the purpose of str[i] and string[i] is for.
Copy linkTweet thisAlerts:
@FotisauthorJan 26.2017 — Hi !!

The purpose is to check the sting[i] values with the match() and then if the string[i] value.length is > than 2 console.log no and if the string[i].length is <=2 to console.log yes.

That is that I am tying to do.

Sorry I am a newbie.

Thank you again for your replies!!
Copy linkTweet thisAlerts:
@rootJan 26.2017 — You still need to clarify what string[i] actually is.



You have not provided any reference to str or string is.



if string is an array of elements, contract them in to a string and use RegExp to make the pattern that match uses, I can't be any more clearer until you have shown me what str[i] and string[i] is.
Copy linkTweet thisAlerts:
@rootJan 26.2017 — You should note that console.log is an output in the browser console (F12 key opens/closes) and output can be found under the log tab. It is good way of debugging or seeing whats happening text wise and allows you to see how a script is executing in the browser as well as see error messages about a script that fails to run.
×

Success!

Help @Fotis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...