• + 0 comments
    public static String funnyString(String s) {
        int n = s.length();
    
        // Since the differences are symmetric, you only need to iterate up to the midpoint of the string
        for (int i = 0; i < n / 2; i++) {
            // Calculate the differences for the forward and backward traversals
            int leftDiff = Math.abs(s.charAt(i) - s.charAt(i + 1));
            int rightDiff = Math.abs(s.charAt(n - i - 1) - s.charAt(n - i - 2));
    
            if (leftDiff != rightDiff) {
                return "Not Funny";
            }                
        }        
    
        return "Funny";
    }