From 7d1a4fc08869fe1338e78c820e9feec16c38803f Mon Sep 17 00:00:00 2001 From: Atharv Joundal <158443883+Atharv2433@users.noreply.github.com> Date: Tue, 3 Dec 2024 23:39:03 +0530 Subject: [PATCH] Add files via upload --- RequiredMoves.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 RequiredMoves.java diff --git a/RequiredMoves.java b/RequiredMoves.java new file mode 100644 index 0000000..bd737a6 --- /dev/null +++ b/RequiredMoves.java @@ -0,0 +1,33 @@ +package Strings; + +import java.util.Scanner; + +public class RequiredMoves { + + public static int fun(String s){ + int tm = 0; + char prev = 'a'; + char arr[] = s.toCharArray(); + + for(char ch : arr){ + int mini2 = Math.min(prev - 'a' + 'z' - ch + 1 , 'z' - prev + ch - 'a' + 1); + System.out.println(mini2); + int mini = Math.min(Math.abs(ch - prev) , mini2); + + tm += mini + 1; + prev = ch; + } + return tm; + } + public static void main(String args[]){ + + + Scanner sc = new Scanner(System.in); + + String s = sc.next(); + + System.out.println(fun(s)); + + } + +}