We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is my java solution.
i just used a single for loop.
1. Counting forward or backward is same so same condition can be applied.
2. In iteration first character of string is at 0 and the first character of reverseString is at string.length()-1;
* String - 0 1 2 3 (indices represented by i)
* Reverse - 3 2 1 0 (which can be obtained by string.length()-i-1)
3. we can have the ascii value of a character just by type casting it to int.
4. In each Iteration absolute difference between ascii values of adjacent characters in string is stored in dif and in reverseString is stored in revdif and just compared them in every iteration if it fails just return "Not Funny" or if the iterations are finished then return "Funny
5. Let me know if there are any corrections in explanation:)
publicstaticStringfunnyString(Strings){// Write your code hereintlen=s.length();for(inti=0;i<len-1;i++){intdif=Math.abs((int)(s.charAt(i)-s.charAt(i+1)));intrevdif=Math.abs((int)(s.charAt(len-i-1)-s.charAt(len-i-2)));if(dif!=revdif)return"Not Funny";}return"Funny";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Funny String
You are viewing a single comment's thread. Return to all comments →
Here is my java solution. i just used a single for loop. 1. Counting forward or backward is same so same condition can be applied. 2. In iteration first character of string is at 0 and the first character of reverseString is at string.length()-1; * String - 0 1 2 3 (indices represented by i) * Reverse - 3 2 1 0 (which can be obtained by string.length()-i-1) 3. we can have the ascii value of a character just by type casting it to int. 4. In each Iteration absolute difference between ascii values of adjacent characters in string is stored in dif and in reverseString is stored in revdif and just compared them in every iteration if it fails just return "Not Funny" or if the iterations are finished then return "Funny 5. Let me know if there are any corrections in explanation:)