Sort by

recency

|

1348 Discussions

|

  • + 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;
    }
    
  • + 0 comments
    # we can go with single loop also
    
    def funnyString(s):
        r = s[::-1]
        compareList = [abs(ord(s[i+1]) - ord(s[i])) if(i+1 < len(s)) else None for i in range(0,len(s))]
        compareList2 = [abs(ord(r[i+1]) - ord(r[i])) if(i+1 < len(r)) else None for i in range(0,len(r))]
        return 'Funny' if(compareList == compareList2) else 'Not Funny'
    
  • + 0 comments

    My Java solution:

    public static String funnyString(String s) {
            //use pointers for start and end of the string 
            //iterate over each string
            int i = 1, j = s.length() - 2;
            while(i <= j){
                //compare abs ascii diff between val adjacent ascending vals
                int leftAbs = Math.abs(s.charAt(i) - s.charAt(i - 1));
                int rightAbs = Math.abs(s.charAt(j) - s.charAt(j + 1));
                //if the diff arent the same, return Not Funny
                if(leftAbs != rightAbs) return "Not Funny";
                i++;
                j--;
            }
            //else return funny
            return "Funny";
        }
    
  • + 0 comments

    Rust:

        let znaki = s.chars().collect::<Vec<char>>();
        let n = s.len();
    
        let mut f = Vec::new();
        let mut s = Vec::new();
        for i in 0..n {
            if i + 1 == n {break;}
            f.push(((znaki[i] as i32) - znaki[i+1] as i32).abs());
            s.push(((znaki[n-i-1] as i32) -znaki[n-i-2] as i32).abs());
        }
        if f == s {
            return "Funny".to_string();
        }
        "Not Funny".to_string()
    }