2337. Java의 Leetcode 솔루션
                                            
                                                
                                                
                                                
                                                
                                                
                                                 2813 단어  java
                    
class Solution { 
    public boolean canChange(String s, String t) { 
        int count = 0; 
        //Checking if equal number of spaces
        for (char c : s.toCharArray()) { 
            if (c == '_')
                count++; 
        } 
        for (char c : t.toCharArray()) { 
            if (c == '_')
                count--;
        } 
        if (count != 0)
            return false;
        return checkIfEqualStringFormed(s, t); 
    } 
    public boolean checkIfEqualStringFormed(String s1, String s2) {
        // Checking if frequency of 'L and 'R' are equal in both string or not.
        int[] freq1 = new int[2];
        int[] freq2 = new int[2];
        for (int i = 0; i < s1.length(); i++) { 
            char s1c = s1.charAt(i); 
            char s2c = s2.charAt(i);
            if (s1c == 'L'){
                freq1[0]++;
            }
            else if(s1c == 'R'){
                freq1[1]++;
            } 
            if (s2c == 'L'){
                freq2[0]++;
            } 
            else if(s2c == 'R'){
                freq2[1]++;
            } 
        } 
        if(freq1[0] != freq2[0] || freq1[1] != freq2[1])
            return false;
        // List to store the indices of 'L' and 'R'.
        List<Integer> start = new ArrayList<>(); 
        List<Integer> target = new ArrayList<>(); 
        for (int i = 0; i < s1.length(); i++) { 
            if (s1.charAt(i) != '_') { 
                start.add(i); 
            } 
            if (s2.charAt(i) != '_') { 
                target.add(i); 
            } 
        }
        for (int i = 0; i < start.size(); i++) {
            // Checking if both string will be equal if we remove '_'
            if (s1.charAt(start.get(i)) != s2.charAt(target.get(i))) { 
                return false; 
            }
            // If character are same, check (negative case) 
            // if character at second string is behind first string character index.
            if (s1.charAt(start.get(i)) == 'L') { 
                if (start.get(i) < target.get(i)) { 
                    return false; 
                }
            } else {
                if (start.get(i) > target.get(i)) { 
                    return false; 
                } 
            } 
        } 
        return true; 
    } 
}
리트코드
 주간 콘테스트
문제의 링크는 여기.....
 https://leetcode.com/contest/weekly-contest-301/problems/move-pieces-to-obtain-a-string/
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(2337. Java의 Leetcode 솔루션), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://dev.to/chiki1601/2337-leetcode-solution-in-java-g67
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
문제의 링크는 여기.....
https://leetcode.com/contest/weekly-contest-301/problems/move-pieces-to-obtain-a-string/
Reference
이 문제에 관하여(2337. Java의 Leetcode 솔루션), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/chiki1601/2337-leetcode-solution-in-java-g67텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)