Sort by

recency

|

1352 Discussions

|

  • + 0 comments
    # Funny String 😂🤣🤪😁
    def funny_string(s):
        i, n = 0, len(s)
        for i in range(n-1):
            if abs( ord(s[i+1]) - ord(s[i]) ) != abs( ord(s[n-i-2]) - ord(s[n-i-1]) ):
                return 'Not Funny'
        return 'Funny'
    
  • + 0 comments

    TypeScript O(n) time and O(1) space solution:

    function funnyString(s: string): string {
        let i = 1
        let j = s.length - 2
        
        while (i < s.length && j > -1) {
            let leftVal = Math.abs(s[i -1].charCodeAt(0) - s[i].charCodeAt(0))
            let rightVal = Math.abs(s[j].charCodeAt(0) - s[j + 1].charCodeAt(0))
            
            if (leftVal !== rightVal) {
                return "Not Funny"
            } 
             
            i++
            j--
        }
        
        return "Funny"
    }
    
  • + 0 comments
    def funnyString(s):
        # Write your code here
        
        start = 0
        end = len(s) - 1
        
        while start < (len(s) - 1) and end > 0:
            first = abs(ord(s[start]) - ord(s[start + 1]))    
            second = abs(ord(s[end]) - ord(s[end - 1]))
            if first != second:
                return "Not Funny"
                
            start += 1
            end -= 1
            
        return "Funny"
    
  • + 0 comments

    Here is problem solution in python java c++ c and javascript - https://programmingoneonone.com/hackerrank-funny-string-problem-solution.html

  • + 0 comments

    Here is my simple c++ solution, video explanation here : https://youtu.be/gcNAo9voHvk.

    string funnyString(string s) {
        string r = s;
        reverse(r.begin(), r.end());
        for(int i = 1; i < s.size(); i++){
            if(abs(s[i]-s[i-1]) != abs(r[i]- r[i-1])) return "Not Funny";
        }
        return "Funny";
    }