Sort by

recency

|

1333 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
    public static String funnyString(String s) {
        
            StringBuilder build = new StringBuilder(s);
            String revStr = build.reverse().toString();
            
            int n = s.length();
            char[] firstStr = s.toCharArray();
            char[] secondStr = revStr.toCharArray();
            
            for(int i=0; i<n-1; i++){
                
                int diff1 = Math.abs((int)firstStr[i] - (int)firstStr[i+1]);
                int diff2 = Math.abs((int)secondStr[i] - (int)secondStr[i+1]);
                
                if(diff1 != diff2){
                    return "Not Funny";
                }
                
            }
            return "Funny";
    
        }
    
  • + 0 comments

    2 javascript solutions.

    Tracking double indexes, not too pleasant to read:

        for (let i = 0, j = s.length-1; i < s.length-1; i++, j--) {
            const [code, codeNext] = [s.charCodeAt(i), s.charCodeAt(i+1)];
            const [reverseCode, reverseCodeNext] = [s.charCodeAt(j), s.charCodeAt(j-1)];
    
            if (Math.abs(code- codeNext) !== Math.abs(reverseCode - reverseCodeNext)){
                return "Not Funny";
            }
        }
    
        return "Funny";
    

    An easier to read solution

    function getDiff (str: string): number[] {
        return Array(str.length-1).fill(0).map((_, i) => {
            const code = str.charCodeAt(i);
            const codeNext = str.charCodeAt(i+1);
            return Math.abs(code - codeNext);
        })
    }
    
    function funnyString(s: string): string {
        // Write your code here
    
        const diff = getDiff(s);
        const reverseDiff = getDiff(s.split("").reverse().join(""));
        
        if (diff.some((el, i)=> el !== reverseDiff[i])) {
            return "Not Funny";
        }
        return "Funny";
    }
    
  • + 0 comments

    sunilllllllllllllll sirrrrrr, winner winner chicken dinner

  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM_)
    import Data.Char (ord)
    
    cDif :: Char -> Char -> Int
    cDif x y = abs (ord x - ord y)
    
    sDif :: String -> [Int]
    sDif s = zipWith cDif s (tail s)
    
    solve :: String -> String
    solve s
        | sDif s == sDif (reverse s) = "Funny"
        | otherwise = "Not Funny"
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            s <- getLine :: IO String
            putStrLn $ solve s