Sort by

recency

|

1351 Discussions

|

  • + 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";
    }
    
  • + 0 comments

    Here is my PHP solution:

    function funnyString($s) {
        // Write your code here
        $s_new = str_split($s);
        $s_new_reverse = array_reverse($s_new);
        $hasil = "Funny";
        
        for ($i=0; $i < count($s_new)-1; $i++) {
            if (abs(ord($s_new[$i]) - ord($s_new[$i+1])) !== abs(ord($s_new_reverse[$i]) - ord($s_new_reverse[$i+1]))) {
                $hasil = "Not Funny";
                break;
            }
        }
        return $hasil;
    }